Any way of capturing the exit url when someone leaves?

Discussion in 'JavaScript' started by mopacfan, Jun 16, 2004.

  1. #1
    Does anyone know of a method to capture the url someone has clicked on when they are leaving your site? I'd like to know the destination of a visitor if they click on an ad or type in a new address. It would be helpful to know where they are going, moreso than just the url of the page they exited from.
     
    mopacfan, Jun 16, 2004 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    You could do it with a JavaScript embedded on all your pages that do something based on when the user leaves. A more common way of doing it would be to make all your links clickable to a script that redirects.
     
    digitalpoint, Jun 16, 2004 IP
  3. mopacfan

    mopacfan Peon

    Messages:
    3,273
    Likes Received:
    164
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I use the redirect on my own web site. For the corporate site I maintain, we don't have many external links, only to our other division's sites. However, I'd like to know if a person goes back to where they came from, types in a new address or clicks on one of our division's sites (don't want to loose the anchor text links for redirection).

    So, in javascript, what method would I use on the onExit function, to capture the url and store it in a database?
     
    mopacfan, Jun 16, 2004 IP
  4. relaxzoolander

    relaxzoolander Peon

    Messages:
    141
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #4
    "types in a new address..."
    now you are getting into privacy issues....
    who do you think you are...google?
    ;)
     
    relaxzoolander, Jan 15, 2005 IP
  5. phrozen_ra

    phrozen_ra Peon

    Messages:
    147
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5

    you must have a PHP that does the database job... and then from javascript you take the url of the current page stored in window.location.href into a variable

    then you pase it to the PHP using an image

    document.getElementById('imageid').src= 'http://path_to_php?exitURL='+window.location.href;

    that should do it... from here is a PHP job
     
    phrozen_ra, Jan 18, 2005 IP
  6. relaxzoolander

    relaxzoolander Peon

    Messages:
    141
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #6
    relaxzoolander, Jan 18, 2005 IP
  7. phrozen_ra

    phrozen_ra Peon

    Messages:
    147
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    :)... funny guy
    you cannot, by any means cannot capture what it's written in the address bar...
    you can capture that only if you press enter after you type in something new

    as for the mail topic starter... if you want to track the url they click on right before they live...

    instead of using window.location.href... use the url in the link they clicked on...
    this means you have to redirect all the links
     
    phrozen_ra, Jan 19, 2005 IP
  8. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #8
    or you can also use a form with a get action that calls an external php file?
     
    daboss, Jan 19, 2005 IP
  9. greensweater

    greensweater Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I found this thread while looking for a solution to this problem. Here is what I've come up with so far. No need to screw around with your link tags -- just add the image tag in your document to direct log data to the PHP script (or whatever). Works with Explorer 6 and Firefox, untested with other browsers.

    
    <html>
    <head>
    <script>
    document.onclick = exitURL; // route all click events
    function exitURL(e) {
       if (!document.getElementById) return true; // DOM only
       var eTag = document.getElementById('tracker');
       var uLog = '/path/to/logscript.php?url=';
       var isLink = false;
    
       if (!e) { 
          var e = window.event; // IE
          var elmClick = e.srcElement;
       } else { 
          var elmClick = e.target; // FF
       }
       while (String(elmClick.nodeName) != "A") {
          elmClick=elmClick.parentNode; // up the tree 
          if (String(elmClick.nodeName) == "HTML") return true;
       }
       url = String(elmClick);
       if (url.indexOf("http") != -1 && url.indexOf(location.hostname) == -1) {
          eTag.onerror = function() { document.location.href = url; }
          eTag.src = uLog + url;
          return false; // wait for log, then leave page
       }
       return true; // pass back to originating object
    }
    </script>
    </head>
    <body>
    <a href='http://www.example.com'>Example</a>
    <img id='tracker' width=0 height=0 border=0>
    </body>
    </html>
    
    Code (markup):
    All click events are routed through this function, which checks to make sure it's an anchor tag. The indexOf checks for http (non-local) links on different hostnames. The onerror event fires when the image fails to load (because it's not really an image), which then directs the browser to the requested page. Should fail cleanly on browsers that don't support it.
     
    greensweater, Nov 29, 2005 IP
  10. alpc

    alpc Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hi Change This code, but not work iframe please help while (String(elmClick.nodeName) == "A") {alert(elmClick);
     
    alpc, Oct 26, 2011 IP