Reputation: 18235
I want to write a global method for my site.
That when the page jumps to another, before the redirection acts, show a waiting mask on the page.
So I'm wondering if there is some way to get an event before the redirect?
The following code explains what I want to do, but doesn't work.
$(function() {
$(document).on('before_redirect', function() {
// show the waiting mask.
});
});
The answer can not be so neat, I just want the GLOBAL METHOD, once this piece of javascript included, no need to write anything other place.
Pray for good solution!
Upvotes: 0
Views: 6088
Reputation: 532465
What you want is the beforeunload or unload event. JQuery also provides the unload shorthand method (deprecated after 1.8). Different browsers handle these events inconsistently, so you'll want to test each and make sure that your solution is acceptable in each. Generally, you'll lose control of the window once you've allowed the request to proceed so a mask may or may not work as intended.
Upvotes: 2
Reputation: 2276
the simplest way is place the same class name
on redirecting elements
like buttons
,anchor tag
like so and write code based on class.
Html
<a class="navigation" href="/nextpage"></a>
or
<button class="navigation"></button> //for this you need to put url onClick function
js
$(function() {
$(document).on('click', '.navigation', function() {
// show the waiting mask.
});
});
Upvotes: 0