onbeforeunload and eval Problems in IE

S

Scott Mitchell

I am using the onbeforeunload client-side event to prompt a user when
leaving a page after they have made changes, as discussed in this
article: Using ASP.NET to Prompt a User to Save When Leaving a Page
[http://aspnet.4guysfromrolla.com/articles/101304-1.aspx]. The problem
I am having is with the page unloading via eval. Specifically, I am
using telerik r.a.d. menu
[http://telerik.com/Default.aspx?PageId=1415], which redirects users to
another page via script like:

eval('window.location.href='url of menu item');

When a user attempts to leave the page via a menu option, they get the
prompt, as expected, but if they click Cancel there is a script error:
Unspecified error on the line number of the eval() function call. This
works just fine in Firefox.

I created a VERY simple page to test this out and confirm that it is
the eval() function. The page below, when visited through IE, displays
the prompt and if I click Cancel I get the same Unspecified error.

<script>
function foo()
{
return "bar";
}

window.onbeforeunload = foo;

eval('window.location.href=\'http://www.google.com\'');
</script>

I was unable to find any info via Google as to if this is a well-known
bug or if there are any workarounds. Any ideas/suggestions/comments
most appreciated. (I'm posting this in the telerik forums as well;
hopefully there's an easy way to get rid of those eval() statements,
but I doubt it... )

TIA
 
S

Scott Mitchell

The eval statement is embedded in the logic for a particular ASP.NET
Web control (telerik's r.a.d. menu). Removing the eval() is not as
simple as you make out. Basically there is a JavaScript function in
the script library for r.a.d. menu that does an eval of what is passed
in, and that particular command - window.location.href... - might be
passed in, depending on a variety of menu settings and so forth.
 
R

Randy Webb

Scott said:
The eval statement is embedded in the logic for a particular ASP.NET
Web control (telerik's r.a.d. menu). Removing the eval() is not as
simple as you make out. Basically there is a JavaScript function in
the script library for r.a.d. menu that does an eval of what is passed
in, and that particular command - window.location.href... - might be
passed in, depending on a variety of menu settings and so forth.

Then get a better menu system.
 
S

Scott Mitchell

So are you saying that onbeforeunload and eval are not compatible with
IE and there's not a known workaround?

Getting a new menu control is probably the last option since, (a)
Money's been dropped on this one, and (b) The menu control is working
great except for this issue on a very large site. Replacing it would
no doubt take considerable time and effort, so I'd like to exhaust this
avenue first.
 
R

Richard Cornford

Scott said:
So are you saying that onbeforeunload and eval are not
compatible with IE and there's not a known workaround?
<snip>

One of the reasons that competent javascript authors do not use the -
eval - function (except under the most rare and extreme circumstances)
is that it obscures errors. If you just remove it and re-try the code
you may get additional insight into the cause of the problem.

Richard.
 
S

Scott Mitchell

I think the *precise* cause of the onbeforeunload error is because the
script that's unloading the document occurs in the eval(). That is, in
the script sample code I sent that causes a problem, if I move the
window.location.href out of the eval(), everything works as expected.

So I believe that I have identified what, precisely, is the root cause
of this problem. I am wondering if there's a known workaround, though,
so I don't have to go through the complete hassle of retooling my site
to use a different menu component.

(I've also asked this question on telerik's support forums, so
hopefully they'll have some suggestions/insight there...)
 
R

Randy Webb

Scott said:
I think the *precise* cause of the onbeforeunload error is because the
script that's unloading the document occurs in the eval(). That is, in
the script sample code I sent that causes a problem, if I move the
window.location.href out of the eval(), everything works as expected.

So I believe that I have identified what, precisely, is the root cause
of this problem. I am wondering if there's a known workaround, though,
so I don't have to go through the complete hassle of retooling my site
to use a different menu component.

The "workaround" was given to you. Remove the eval, "everything works as
expected", and everybody is happy.
 
S

Scott Mitchell

Randy, is that the only workaround? As I explained earlier, that is
not a viable option. Are there any other workarounds you know of? Can
you (or someone else) point me to a URL that explains this
onbeforeunload and eval() problem in IE in more detail?

Thanks.
 
R

Richard Cornford

Scott said:
Randy, is that the only workaround? As I explained earlier,
that is not a viable option. Are there any other workarounds
you know of? Can you (or someone else) point me to a URL that
explains this onbeforeunload and eval() problem in IE in
more detail?

When I tested your code last night removing the - eval - function
resulted in code that exhibited exactly the same behaviour and threw
exactly the same exceptions. Which suggests that there is no
incompatibility problem with the use of - eval - in this context, and so
there can be no work around for its use.

Richard.
 
S

Scott Mitchell

I have to admit that this is probably the least useful newsgroup I've
posted to. Good thing I don't do client-side development for a living.

Anywho, a workaround to this problem was to place the eval() statements
in try{}catch{} blocks, which would suppress the JavaScript error.
 
R

Robert Aldwinckle

(posted from ie6.browser on msnews; I don't have access to USENET
but am leaving the cross-posting as is in case it propagates)

Scott Mitchell said:
I think the *precise* cause of the onbeforeunload error is because the
script that's unloading the document occurs in the eval(). That is, in
the script sample code I sent that causes a problem, if I move the
window.location.href out of the eval(), everything works as expected.

So I believe that I have identified what, precisely, is the root cause
of this problem. I am wondering if there's a known workaround, though,
so I don't have to go through the complete hassle of retooling my site
to use a different menu component.


Scott,

ie6.browser is not a good newsgroup for discussing developer
issues such as this. I would try one of the MSDN newsgroups
which specialize in web development or scripting.

http://msdn.microsoft.com/newsgroups/


HTH

Robert Aldwinckle
---
 
T

Thomas 'PointedEars' Lahn

Scott said:
I have to admit that this is probably the least useful newsgroup I've
posted to. Good thing I don't do client-side development for a living.

Anywho, a workaround to this problem was to place the eval() statements
in try{}catch{} blocks, which would suppress the JavaScript error.

Your incompetence and lack of courtesy has clouded your judgement
which resulted in curing the symptoms rather than the illness. If
I would have more time, I would pity you.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top