-2

I want to use the onbeforeunload only when the site is being redirected to a particular page. How can I do that?

1 Answers1

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