加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

html – IE中的window.onbeforeunload和window.location.href

发布时间:2020-12-14 23:52:02 所属栏目:资源 来源:网络整理
导读:我们使用window.location.href将用户导航到页面. 此外,我们已配置window.onbeforeunload事件,以便在存在任何未保存的更改时提醒用户. window.onbeforeunload = confirmBeforeClose;function confirmBeforeClose() { if (jwd.global.inEditMode) return "Your
我们使用window.location.href将用户导航到页面.
此外,我们已配置window.onbeforeunload事件,以便在存在任何未保存的更改时提醒用户.
window.onbeforeunload = confirmBeforeClose;

function confirmBeforeClose() {
    if (jwd.global.inEditMode)
        return "Your changes will not be saved :) and you will be punished to death";
}

在有未保存更改的地方,我尝试使用window.location.href来导航用户,我收到警报消息.

如果我在弹出窗口中单击“确定”,它可以正常工作.但是,如果我单击CANCEL,JS会在window.location.href中抛出一个未指定的错误.

任何帮助表示赞赏.

解决方法

我也遇到了这个问题(在IE7及更高版本中,但不是在IE6中).

我能找到的唯一解决方案是在try / catch块中包装window.location.href调用.

以下是重现问题的完整示例.如果您取消注释try / catch,那么它在所有浏览器中都可以正常工作.

JavaScript(HTML头文件):

window.onbeforeunload = confirmBeforeClose;

  function confirmBeforeClose( )
  {
    return 'You have made changes on this page that will be lost if you navigate away without saving.';
  }

  function leavePage( )
  {
     // try {
          window.location.href = "http://www.example.com";
     // } catch( e ) { }
  }

HTML:

<body>
 <a href="#" onclick="leavePage(); return false;">Leave this page</a> 
</body>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读