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.