SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Location
    Portugal
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question onclick with href problem..

    Hi,

    I am building a directory site ( mostly a resource list of sites ) and i use the following instruction for all the links of this site :

    <a href="http://www.sitepoint.com/" onclick="return gourl(118)">Sitepoint</a>

    where gourl(id) is a simple function that uses linkcount.php to redirect the url - I use this way so I can have a outgoing hit counter for each of the sites and i use href ... for SEO proposes and for visitor reading.

    <script type="text/javascript">
    function gourl(id) {
    document.location.href = '/linkcount.php?id='+id;
    return false;
    }
    </script>


    linkcount.php connects to mysql and ads 1 click to the id hitcount row and then sends visitor to site ( header("Location:$row[url]"); )

    The problem is if i click on link using shift ou Ctrl not only will a new tab or window open with the correct site but my site also opens this new url.

    I am using return false; on the gourl(id) function so that only the onclick event works but seems i am doing some thing wrong here.

    I appreciate all the help,
    Thanks,
    Pedro

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I coulnt understand why you have assigned href, if you assign the some page in href then it will also be triggered when you click the link. Why do you need that href link http://www.sitepoint.com? So the following should work:
    HTML Code:
    <a href="javascript:void(null);" onclick="return gourl(118, 'http://www.sitepoint.com')">Sitepoint</a>
    Code javascript:
    function gourl(id, url) {
         document.location.href = '/linkcount.php?id=' + id + '&url=' + url;
         return false;
    }

    Then when you update your database in linkcount.php then redirect again to sitepoint.

    linkcount.php
    Code php:
    # do some updates 
    header('Location: ' . $_GET['url']);
    exit();
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Member
    Join Date
    Oct 2007
    Location
    Portugal
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi rajuq,

    I use also href so when visitors pass the mouse hover the link they see in bottom tab the url they will go.

    This is also used in http://www.forwebdesigners.com/

    and i want to do the same.

    Also, and i not sure, for SEO proposes and for google bot to follow all links in my site it needs href...

    I thought that return false should stop the href and will only go for the onclick event.


    Ty in advance for the help
    Pedro

  4. #4
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's important to keep in mind that it's impractical to expect JS-enabled features to be foolproof. For example, if the user right clicks on the link and chooses 'Open in new Tab' or 'Open in new Window', the function gourl() won't be called at all and your site will fail to count a hit. The same thing happens if the JavaScript is disabled/missing in the browser.

    Nonetheless, can you show us the URL of the live page where shift+clicking is opening two page at once?


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •