Scott on Writing

Musings on technical writing...

onbeforeunload and eval Problems in IE

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.

posted on Tuesday, April 19, 2005 11:11 AM

Feedback

# re: onbeforeunload and eval Problems in IE 5/31/2005 1:32 PM Scott English

I'm encountering this problem as well. I'm using the onbeforeunload event to warn the user that they are about to discard their changes. I also get the "Unspecified error" when I select an item from the menu. However, I'm not using telek's menu control, but rather (ahem) skmMenu 2.2.

When I get the error and launch the debugger, I stops on the menu item attribute:

onclick="javascript:skm_closeSubMenus(document.getElementById('TopNav'));location.href='/CustomReporting/Custom/Action3.aspx';"

Specifically, it stops on the line that sets the location.href property. I don't see any eval() function, but I'm getting the same result. Do you know any way to fix this?

I'm using Windows XP SP2.

# re: onbeforeunload and eval Problems in IE 7/28/2005 9:17 AM Tristan Bates

Just found another possible workaround that will prevent you asking control vendors to "fix" their code if you encounter this bug.

In the function that handles the onbeforeunload event, just before you return the message to display in the dialog do:

window.onerror = handleError;

And then write a function:

function handleError()
{
return true;
}

And that will silently capture and handle the unspecified error.

Its not particularly elegant either but then none of this onbeforeunload stuff is. But at least this lets you deal with the error in your own code.

I agree with the previous post. I'm not sure that this bug is just about eval(). I get the same error when I do a document.location.href in a plain javascript function and also when doing a document.forms[1].submit();

# re: onbeforeunload and eval Problems in IE 9/2/2005 8:45 AM Graham McDonald

I'm experiencing a different problem using the onbeforeunload event to warn the user that they are about to discard their changes (exactly as in the original article): if the page contains Validator controls and these cause the 'Save' to be rejected, any subsequent attempt to navigate away from the page (without making additional changes) doesn't display the warning prompt.

This seems to be because btnSave_onClick fires (sets needToConfirm to false) before the Validators get to work...

# re: onbeforeunload and eval Problems in IE 5/24/2006 12:36 PM James Ballaban

I've had this issue before. It does not relate directly to eval. If you create a test page you will notice that putting onbeforeunload=function() { "Are you sure?"; } and clicking on <div onclick="location.href='here';">Click</div> will generate an error if you press cancel. If it was a normal anchor tag or a page refresh it would work properly. This only happens to IE.

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55518.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3019100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3019100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.97159
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403019

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.64321
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403019

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles