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?
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With