javascript - How can I override the OnBeforeUnload dialog and replace it with my own? -


i need warn users unsaved changes before leave page (a pretty common problem).

window.onbeforeunload=handler 

this works raises default dialog irritating standard message wraps own text. need either replace standard message, text clear, or (even better) replace entire dialog modal dialog using jquery.

so far have failed , haven't found else seems have answer. possible?

javascript in page:

<script type="text/javascript">       window.onbeforeunload=closeit; </script> 

the closeit() function:

function closeit() {   if (changes == "true" || files == "true")   {       return "here can append custom message default dialog.";   } } 

using jquery , jqmodal have tried kind of thing (using custom confirm dialog):

$(window).beforeunload(function() {         confirm('new message: ' + this.href + ' !', this.href);         return false;     }); 

which doesn't work - cannot seem bind beforeunload event.

you can't modify default dialogue onbeforeunload, best bet may work it.

window.onbeforeunload = function() {     return 'you have unsaved changes!'; } 

here's reference microsoft:

when string assigned returnvalue property of window.event, dialog box appears gives users option stay on current page , retain string assigned it. default statement appears in dialog box, "are sure want navigate away page? ... press ok continue, or cancel stay on current page.", cannot removed or altered.

the problem seems be:

  1. when onbeforeunload called, take return value of handler window.event.returnvalue.
  2. it parse return value string (unless null).
  3. since false parsed string, dialogue box fire, pass appropriate true/false.

the result is, there doesn't seem way of assigning false onbeforeunload prevent default dialogue.

additional notes on jquery:

  • setting event in jquery may problematic, allows other onbeforeunload events occur well. if wish unload event occur i'd stick plain ol' javascript it.
  • jquery doesn't have shortcut onbeforeunload you'd have use generic bind syntax.

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

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -