0

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.

Alex
  • 1,914
  • 6
  • 26
  • 47

1 Answers1

0

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

Community
  • 1
  • 1
Reflective
  • 3,854
  • 1
  • 13
  • 25
  • I don't want to change onbeforeunload handler of the page, I want to log onbeforeunload or any other event that would be trigger on leaving a page. – Alex Jul 18 '15 at 18:58