469,360 Members | 1,728 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

onbeforeunload and eval Problems in IE

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/art...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

Jul 23 '05 #1
12 8235
Scott Mitchell wrote:
<snip>
eval('window.location.href=\'http://www.google.com\'');
</script> <snip> hopefully there's an easy way to get rid of those eval()
statements, but I doubt it... )


No, its easy to get rid of that - eval - call. You just remove it:-

window.location.href = 'http://www.google.com';

Richard.
Jul 23 '05 #2
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.

Jul 23 '05 #3
Scott Mitchell wrote:
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #4
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.

Jul 23 '05 #5
Scott Mitchell wrote:
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.
Jul 23 '05 #6
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...)

Jul 23 '05 #7
Scott Mitchell wrote:
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #8
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.

Jul 23 '05 #9
Scott Mitchell wrote:
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.
Jul 23 '05 #10
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.

Jul 23 '05 #11
(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" <sc**************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
---


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


Jul 23 '05 #12
Scott Mitchell wrote:
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
Jul 23 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by lltaylor | last post: by
6 posts views Thread by jennyw | last post: by
reply views Thread by Rick Lubanovic | last post: by
4 posts views Thread by sowencheung | last post: by
8 posts views Thread by drewmania001 | last post: by
1 post views Thread by David | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.