0

Getting the referer page is possible in javascript with document.referer.

But is it possible to get the exit page in javascript ?

For example, my website is example.com. A visitor visits the page exemple.com, then he types in the url bar "www.aaaaaaa.com" to leave my website : i want to detect this url wwww.aaaaaaa.com by javascript.

Thank you in advance, cordially.

totoaussi
  • 712
  • 1
  • 11
  • 27
  • 3
    What do you mean by `exit page`? – Tushar Jul 21 '15 at 10:20
  • Hello, I give more information in my question. For example, if a visitor visits your website then leave your website by typing a other website url in the url bar, the javascript must detect this exit url. – totoaussi Jul 21 '15 at 10:25
  • 2
    You can't. If the visitor is not on your website anymore (exemple.com), you can't see what he/she is doing. – putvande Jul 21 '15 at 10:26
  • `window.onbeforeunload = function() { alert(window.location.href); };` you can try this. however, not sure it it will work in every condition – Tushar Jul 21 '15 at 10:27
  • Thank Tushar, but window.location.href detects the page from which the visitor leaves the website, not the new url. Imagine your visitor visits your website example.com, the leave your website by typing "foo.com" in the url bar : i want javascript detects this foo.com – totoaussi Jul 21 '15 at 10:36
  • What is your reason for wanting this? – Popnoodles Jul 21 '15 at 12:14
  • To track the exit page for my web statistic. – totoaussi Jul 21 '15 at 12:27

2 Answers2

0

Try the onbeforeunload event. It is fired just before the page is unloaded.

window.onbeforeunload = function (e) {
var exitPage =window.location.href ;
}

Save Exit Page in your cookie and to detect an address bar is changed you can check the current address periodically using setTimeout/interval:

var oldLocation = window.location.hostname;
setInterval(function() {
  if(window.location.hostname != oldLocation) {
       //do your action
      // oldLocation = location.href
  }
}, 1000); // check every second

But in this case you need to check domain name, because some times URL comes with different parameter.

Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
0

In order to track the exit page for your web statistics you will just link to another forwarding page on your server and track the usage of that page. That's how it's generally done.

Alternatively, if you might consider using Google Analytics check this code:

<script>
/**
* Function that tracks a click on an outbound link in Google Analytics.
* This function takes a valid URL string as an argument, and uses that
* URL string as the event label.
*/
var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
     function () {
     document.location = url;
     }
   });
}
</script>

Usage:

<a href="http://www.example.com"
   onclick="trackOutboundLink('http://www.example.com'); return false;">
Check out example.com
</a>

The code above was copied from this page and from here: Google Analytics Outbound Link Event Tracking

Community
  • 1
  • 1
NilsB
  • 1,154
  • 1
  • 16
  • 42