Just wrapped up this week's article for 4Guys which takes a look at some common questions/problems readers have alerted me to regarding a past article of mine, Using ASP.NET to Prompt a User to Save When Leaving a Page. That previous article examined how to use a the onbeforeunload client-side event along with some JavaScript to determine if a user was leaving a page after having modified some data-entry input fields but before having saved the changes. If the user attempted to leave the page without having saved their changes - either by clicking on a link in the page, attempting to close the browser, having the browser “hijacked“ by clicking on a link in another program, etc. - a prompt like the one shown below would magically appear, warning the user that they were about to leave the page without having saved their latest changes, letting them cancel leaving the page.
The previous article examined how to wrap up this client-side functionality into an ASP.NET base class that included methods to indicate what Web controls needed to be monitored for changes and what Button controls, when clicked, shouldn't trigger the client-side check (buttons like Save and Cancel shouldn't prompt the user, for example).
Over the past several months I have gotten dozens of emails from readers regarding that particular article. The most common has been having the prompt displayed when a Web control with its AutoPostback property set to True was changed. A fix for this is discussed in this week's article (An Update on Prompting a User to Save When Leaving an ASP.NET Page).
The other common question that I received was along the lines of, “I'm using a menu control and when I use the menu control to leave a page the prompt appears, as expected. However, when I click Cancel, to stay on the page, I see an 'Unspecified error' script error. Help!” I actually wasn't able to help much until recently, when by fortuitous circumstance I happened to be working on a project that used telerik's r.a.d. menu. This menu control exhibited the “Unspecified error” script errors others had notified me of when using onbeforeunload, and with a bit of investigation I found out why - r.a.d. menu uses eval() statements to redirect users to a different menu choice, and (for some reason) Internet Explorer doesn't like onbeforeunload and eval() statements.
In the end, telerik provided a workaround, although it's a bit of a cheeky workaround. I ended up having to surround the eval() statements with an empty try...catch block, like:
try { eval(...); } catch (blah) {}
That suppressed the script error messages in IE. (FireFox didn't freak out over this in the first place...) For more info, you can see a forum entry I made on this topic.