-1

I need to stop redirect to a particular url. There is a piece of encrypted async js code which redirects to another page on adding any external/3rd party js. Is it possible to have an event or anything which can detect the redirect to that specific url alone? I have used this code

var back = false;
back = true; //Somewhere, the condition is set to true
window.onbeforeunload = function (e) {
    if(back == true)
        return "Are you sure to exit?";
}

Is it possible to autopress cancel after popup?

1 Answers1

0

You can prevent the page from navigating away, however, a dialog box will always get triggered that will ask the user if they want to leave.

I think this is one of those "human interaction" things that browser enforce because this feature could potentially cause a browser to be stuck forever on one web page/site.

window.addEventListener("beforeunload", function(evt){

   // Didn't see anything in the event that eluded to the intended URL to navigate to.
   // So filtering the navigation based on the destination seems impossible?
   for(var prop in evt){
       console.log(prop, evt[prop]);
   }
   evt.preventDefault(); // prevents navigation (which is the default action)
   evt.returnValue = true; // for chrome

}, true); // "use capture" = true
bob
  • 7,539
  • 2
  • 46
  • 42