javascript - jQuery trigger beforeunload event programmatically -


i trying invoke browser's onbeforeupload event jquery under:-

<script type="text/javascript">  $(document).ready(function (){     $(window).trigger('beforeunload'); })  </script> 

it doesnt display browser's default onunload event confirm box means beforeunload event not getting fired.

how can trigger beforeunload event?

thanks

why think showing confirmation dialog default behavior?

the api says:

when event returns non-void value, user prompted confirm page unload. in browsers, return value of event displayed in dialog. in firefox 4 , later returned string not displayed user. instead, firefox displays string "this page asking confirm want leave - data have entered may not saved." see bug 588292.

since 25 may 2011, html5 specification states calls window.alert(), window.confirm(), , window.prompt() methods may ignored during event. see html5 specification more details.

note various mobile browsers ignore result of event (that is, not ask user confirmation). firefox has hidden preference in about:config same. in essence means user confirms document may unloaded.

you can , should handle event through window.addeventlistener() , beforeunload event. more documentation available there.

because of that, need add handler event beforehand:

$(window).on('beforeunload', function(){    ... }); 

other ways:


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -