log the alert box that pops up when leaving a page <p>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? <strong>Edit</strong> 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:</p> <p><div class="snippet" data-lang="js" data-hide="false"> <div class="snippet-code"> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;html&gt; &lt;head&gt; &lt;script&gt; window.alert = function(str){console.log(‘alert is called: ’ + str);}&lt;/script&gt; &lt;/head&gt; &lt;body onbeforeunload=function(){alert(“you are leaving!”);}&gt; Sample page &lt;/body&gt; &lt;/html&gt;</code></pre> </div> </div> </p> <p>When I execute window.location='<a href="http://google.com" rel="nofollow">http://google.com</a>' on this page, an alert box pops up, instead of calling that overwritten alert function.</p>