Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture leaving page - jQuery

I would like to try and capture the page to which a user to leaving to.

I'm using the following in order to capture the event of a user leaving the page:

$(window).bind('beforeunload', function() {

});

How do I figure out what page the user has asked the browser to load?

All I can think of is something like this:

$('a')
.click(function(){
    var url = $(this).attr('href');
});

or

$('form')
.submit(function(){
    var url = $(this).attr('action');
});

Any ideas?

like image 439
RadiantHex Avatar asked Feb 11 '26 20:02

RadiantHex


1 Answers

For security reasons, JavaScript will have no access to what the user is typing in the URL bar.

But if you mean that they are clicking a link on your page to get to the new page, then you can do this:

$('a').click(function(){
    var nextPage = $(this).attr('href');
    // Then ajax nextPage to your server
});

Or if the redirect is in the JavaScript setting of window.location.href, then do this:

$(window).unload(function(){
    var nextPage = window.location.href;
    // Then ajax nextPage to your server
});
like image 145
jairajs89 Avatar answered Feb 16 '26 03:02

jairajs89



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!