简体   繁体   中英

log the alert box that pops up when leaving a page

I want to log alert boxes that pop up when I leave a page(when onbeforeunload, onunload or any other event is triggered while leaving a page). So I have overwritten alert function to record alert function invocations. Then I set window.location to some other url to navigate away from the page. But the problem is that when window.location is executed, it destroys my custom alert function and I can not log it any more. Any suggestion on how to solve it? Edit The page that I want to log its alert box is a third party's page. In order to inspect it, a scrip code is injected to the header of the page like this:

 <html> <head> <script> window.alert = function(str){console.log('alert is called: ' + str);}</script> </head> <body onbeforeunload=function(){alert(“you are leaving!”);}> Sample page </body> </html> 

When I execute window.location=' http://google.com ' on this page, an alert box pops up, instead of calling that overwritten alert function.

This could save some efforts catching the leave page event:

window.onbeforeunload = function (e) {
  var message = "Your confirmation message goes here.",
  e = e || window.event;
  // For IE and Firefox
  if (e) {
    e.returnValue = message;
  }

  // For Safari
  return message;
};

the answer is not my property, you can find more here:

Intercept page exit event

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM