Page Capture - jQuery

I would like to try to capture the page the user is going to.

I use the following to capture the event of a user leaving the page:

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

});

How to find out which page the user has loaded the browser on?

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?

+3
source share
1 answer

For security reasons, JavaScript will not have access to what the user types in the URL string.

But if you mean that they click a link on your page to go to a new page, you can do this:

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

Or if the redirection is in your JavaScript window.location.href setting, do the following:

$(window).unload(function(){
    var nextPage = window.location.href;
    // Then ajax nextPage to your server
});
+3
source

Source: https://habr.com/ru/post/1790349/


All Articles