onbeforeunload消息出现两次(Internet Explorer) [英] onbeforeunload Message Appears Twice (Internet Explorer)

查看:95
本文介绍了onbeforeunload消息出现两次(Internet Explorer)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ASP.NET网页上有以下脚本,用于在用户离开未保存更改的页面时向用户发出警告。

I have the following script on my ASP.NET web page to warn the user when they are navigating away from a page with unsaved changes.

$(document).ready(function() {
    $('input:not(:button,:submit),textarea,select').change(function () {
        window.onbeforeunload = function () {
            return 'Changes made to this page have not been saved!';
        }
    });
});

除了用户使用Internet Explorer并尝试导航时出现的一些问题外,这个工作方式正常使用类似这样的链接。

This works just as intended except for some problems when the user is using Internet Explorer and attempts to navigate away using a link like this one.

<div onclick="window.location=&#39;http://www.domain.com&#39;;">Text</div>

如果用户选择留在此页面上,则会出现两次消息框。然后,如果他们第二次选择Stay on this page,调试器会突出显示上面的链接并显示以下消息:

If the user selects "Stay on this page", the message box appears twice. And then if they select "Stay on this page" the second time, the debugger highlights the link above with the message:


Microsoft JScript运行时错误:未指定错误。

Microsoft JScript runtime error: Unspecified error.

此问题似乎特定于Internet Explorer。在网上搜索,我已经能够找到许多类似错误的参考资料,令人难以置信地回溯了几个版本的Internet Explorer。然而,我无法找到微软的任何评论或合理的解决方案。以下是我发现的一些相关链接。

This problem appears to be specific to Internet Explorer. Searching the Web, I've been able to find numerous references to what seem like related bugs, incredibly going back several versions of Internet Explorer. Yet, I've unable to find any comment from Microsoft or a reasonable fix. Here are a few of the links I've found that appear related.


  1. http://forums.asp.net/t/1199756.aspx/1

  2. < a href =https://stackoverflow.com/questions/1010681/why-is-my-onbeforeunload-handler-causing-an-unspecified-error-error>为什么我的onbeforeunload处理程序导致未指定的错误?错误?

  3. http://www.telerik.com/community/forums/aspnet-ajax/tabstrip/cancel-on-onbeforeunload-in -ie-6-7-create-js-unspecified-error.aspx

  4. http://scottonwriting.net/sowblog/archive/2005/04/19/163068.aspx

  5. http ://www.techtalkz.com/internet-explorer/116662-onbeforeunload-dialog-cancel-button-when-window-location-href-bug.html

  6. < a href =http://www.ibm.com/developerworks/forums/thread.jspa?messageID=14213927 =nofollow noreferrer> http://www.ibm.com/developerworks/forums/thread.jspa ?MESSAGEID = 14213927

  7. http ://www.webmasterkb.com/Uwe/Forum.aspx/jscript/4321/IE7-and-onbeforeunload

  8. http://www.bigresource.com/ASP-Javascript-Unspecified-Error-BHoy7ptS.html

  9. http://www.4guysfromrolla.com/articles /042005-1.aspx

  1. http://forums.asp.net/t/1199756.aspx/1
  2. Why is my onbeforeunload handler causing an "Unspecified error" error?
  3. http://www.telerik.com/community/forums/aspnet-ajax/tabstrip/cancel-on-onbeforeunload-in-ie-6-7-create-js-unspecified-error.aspx
  4. http://scottonwriting.net/sowblog/archive/2005/04/19/163068.aspx
  5. http://www.techtalkz.com/internet-explorer/116662-onbeforeunload-dialog-cancel-button-when-window-location-href-bug.html
  6. http://www.ibm.com/developerworks/forums/thread.jspa?messageID=14213927
  7. http://www.webmasterkb.com/Uwe/Forum.aspx/jscript/4321/IE7-and-onbeforeunload
  8. http://www.bigresource.com/ASP-Javascript-Unspecified-Error-BHoy7ptS.html
  9. http://www.4guysfromrolla.com/articles/042005-1.aspx

有人可以建议解决方法吗?看起来Connect不接受IE错误报告。我正在使用IE9

Can anyone suggest a workaround? Doesn't look like Connect is accepting IE bug reports. I'm using IE9

推荐答案

我们在IE11中遇到了同样的问题。

We ran into the same issue in IE11.

事实证明,分配 window.location =http://www.domain.com会在IE中导致此问题。

It turns out that assigning window.location = "http://www.domain.com" will cause this problem in IE.

如果您改为使用 location.href =http://www.domain.com,它只会提示用户一次。

If you instead use location.href = "http://www.domain.com", it will only prompt the user once.

希望这可以帮助别人!

这篇关于onbeforeunload消息出现两次(Internet Explorer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆