I have the following script on my ASP.NET web page to warn the user when they are navigating away from a page with unsaved changes.
$(document).ready(function() {
$('input:not(:button,:submit),textarea,select').change(function () {
window.onbeforeunload = function () {
return 'Changes made to this page have not been saved!';
}
});
});
This works just as intended except for some problems when the user is using Internet Explorer and attempts to navigate away using a link like this one.
<div onclick="window.location='http://www.domain.com';">Text</div>
If the user selects "Stay on this page", the message box appears twice. And then if they select "Stay on this page" the second time, the debugger highlights the link above with the message:
Microsoft JScript runtime error: Unspecified error.
This problem appears to be specific to Internet Explorer. Searching the Web, I've been able to find numerous references to what seem like related bugs, incredibly going back several versions of Internet Explorer. Yet, I've unable to find any comment from Microsoft or a reasonable fix. Here are a few of the links I've found that appear related.
- http://forums.asp.net/t/1199756.aspx/1
- Why is my onbeforeunload handler causing an "Unspecified error" error?
- http://www.telerik.com/community/forums/aspnet-ajax/tabstrip/cancel-on-onbeforeunload-in-ie-6-7-create-js-unspecified-error.aspx
- http://scottonwriting.net/sowblog/archive/2005/04/19/163068.aspx
- http://www.techtalkz.com/internet-explorer/116662-onbeforeunload-dialog-cancel-button-when-window-location-href-bug.html
- http://www.ibm.com/developerworks/forums/thread.jspa?messageID=14213927
- http://www.webmasterkb.com/Uwe/Forum.aspx/jscript/4321/IE7-and-onbeforeunload
- http://www.bigresource.com/ASP-Javascript-Unspecified-Error-BHoy7ptS.html
- http://www.4guysfromrolla.com/articles/042005-1.aspx
Can anyone suggest a workaround? Doesn't look like Connect is accepting IE bug reports. I'm using IE9