Is there a jQuery event to change the page?

I am starting to work in jQuery and trying to get used to it. Is there any event that I can bind to a page to stop my slider?

What I want to do, when the user changes the page or goes to another tab, I want to catch this event. Is there any event or listener for this system?

+4
source share
3 answers

I would go with onbeforeunload instead of unload to give you maximum time. However, if you plan to make a request back to the server at this time (or do something asynchronously), you may find yourself in a race state, where sometimes the request breaks through, and sometimes not.

This is because the page stops processing your request (or other asynchronous action) when it is unloaded. It can be almost immediately in modern browsers and pretty fast in older IEs. Because of this, it is very rare to do something in any of the unload or beforeunload .

If you are trying to pass timer information to the following URL, you can simply change the URLs when clicked.

 $('a').click(function(){ this.href=this.href + "#timer=" + timerVar; }); 

Then, when you go to the next page, you can read this last value from the URL.

Google analytics solves this problem by adding a delay to all links:

 $('a').click(function(){ var that = this; setTimeout(function(){ window.location=that.href; },100); return false; }); 

It just gives your page extra time to follow links. However, it is not reliable, but it does not matter for google, since analytics is somewhat transient (and they also use URL parameters for other more important issues (for example, campaigns)).

+2
source

Take a look at this: http://api.jquery.com/unload/

+1
source
0
source

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


All Articles