javascript - OnUnload Alert Error "NS_ERROR_NOT_AVAILABLE" -
<html> <body> <button type="button" onclick="clickme()">click me</button> <script> var test = 0; function clickme() { test = 1; console.log(test); } window.onunload = function() { alert("test"); } </script> </body> </html>
i'm using simple code test things onunload , onbeforeunload. reason whenever refresh/leave page , cause onunload event no alert , error in firebug console. if use onbeforeunload works , no error, hear onbeforeunload isn't cross-browser.
ns_error_not_available: component returned failure code: 0x80040111 (ns_error_not_available) [nsidomwindow.alert] alert("test");
i not trying alert test variable, text "test" before tries point out.
if want work, have in onbeforeunload event, instead of creating alert/confirm pop-up, onbeforeunload event has built in pop-up. have return string , pop-up appear when user tries navigate away page. if there no return variable, there no pop-up.
- the great thing pop-up message has 2 buttons: ok , cancel.
- if user hits ok, browser continue navigate away page
- if user hits cancel, browser cancel unload , stay on current page
- the onbeforeunload event pop-up can cancel onunload event
an example below:
<script type="text/javascript"> window.onbeforeunload=before; window.onunload=after; function before(evt) { return "this appear in dialog box along other default text"; //if return statement not here, other code executed silently (with no pop-up) } function after(evt) { //this event fires fast application execute before browser unloads } </script>
it seems trying alert in onunload event. issue here it's late. page unloading , there no stopping it. may able alert message show, doesn't matter user clicks because page unloading.
your best bet go onbeforeunload event.
Comments
Post a Comment