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   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles