I want to use the onbeforeunload only when the site is being redirected to a particular page. How can I do that?
Asked
Active
Viewed 62 times
-2
-
Only if that redirect is being done by a user event ( click on link for example) or your own code. Is that the case? – charlietfl Jul 15 '19 at 14:35
-
@charlietfl Yes that is the case. – Uzair Ahmed Jul 15 '19 at 14:36
1 Answers
0
You could set a global variable flag when a link with the appropriate href is clicked.
var doUnload = false;
$(window).on('beforeunload', function(){
if(doUnload){
// whatever it is you want to do
}
});
$(window).on('click', 'a', function(){
// parse href for match
if(this.pathname === '/foo/bar/'){
doUnload = true;
}
})

charlietfl
- 170,828
- 13
- 121
- 150