onbeforeunload事件在IE9中过于热烈 [英] onbeforeunload event is too enthusiastic in IE9

查看:92
本文介绍了onbeforeunload事件在IE9中过于热烈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一些示例测试html:

Here's some sample test html:

<!DOCTYPE html>
<html>
  <body>
    <a href="javascript:alert('Not going anywhere!');">Go nowhere 1</a>
    <a href="javascript:void(0);" onclick="alert('Not going anywhere!');">Go nowhere 2</a>
    <a href="http://www.google.com">Go somewhere</a>
    <script type="text/javascript">
      window.onbeforeunload = function() { return "Really leave?"; };
    </script>
  </body>
</html>

这是一个工作页面: timjeanes.com/ie9_onbeforeunload.htm

在Firefox,Chrome和Safari中,我得到了我想要的行为。也就是说,单击前两个链接中的任何一个都会显示一个警告对话框;单击第三个链接(或以其他方式导航)显示您确定要离开此页面吗?消息。

In Firefox, Chrome and Safari, I get the behaviour I want. That is, clicking either of the first two links shows an alert dialog; clicking the third link (or otherwise navigating away) shows the "Are you sure you want to leave this page?" message.

然而,在IE9中,你确定要离开这个页面吗?当你点击前两个链接中的任何一个时,即使没有正在进行导航,也会显示消息 - 我们只是运行一些javascript。

However, in IE9, the "Are you sure you want to leave this page?" message also shows when you click either of the first two links, even though no navigation is taking place - we're just running some javascript.

我有什么东西吗?做错了?或者IE有一个很好的解决方法吗?

Is there something I'm doing wrong? Or is there a nice workaround for IE?

一个选项是使用href =#并将我的javascript放入onclick。我并不热衷于此,因为它将用户带到了页面的顶部,而我的真实页面非常高。

One option would be to use href="#" and put my javascript in the onclick. I'm not keen on that as it takes the user to the top of the page, and my real-life page is quite tall.

我还没有测试过其他版本的IE。

I've not tested in other versions of IE.

推荐答案

这是一个非常不幸的错误。看来你必须解决它,使用哈希链接方法可能是最好的方法。要避免将用户带到页面顶部,请使用 event.preventDefault() event.returnValue = false

That's a pretty unfortunate bug. It seems you'll have to work around it, and using the hash link method is probably the best way. To avoid taking the user to the top of the page, cancel the event using event.preventDefault() and event.returnValue = false.

function myClickHandler(e) {
    if (e.preventDefault)
        e.preventDefault();
    e.returnValue = false;
    alert("Not going anywhere!");
}

这样称呼:

<a href="#" onclick="myClickHandler(event)">Go nowhere 1</a>

编辑:您可以取消所有哈希链接的事件您的网页是这样的:

You could cancel the event for all the hash-links on your page like this:

var links = document.links;
for (var i = 0; i < links.length; i++) {
    var link = links[i];
    if (link.href == "#") {
        if (link.addEventListener) {
            link.addEventListener("click", cancelEvent, false);
        }
        else if (link.attachEvent) {
            link.attachEvent("onclick", cancelEvent);
        }
    }
}
function cancelEvent(e) {
    e = e || window.event;
    if (e.preventDefault)
        e.preventDefault();
    e.returnValue = false;
}

或者jQuery只使用一行代码:

Or with jQuery using just one line of code:

$("a[href='#']").click(function (e) { e.preventDefault(); });

这篇关于onbeforeunload事件在IE9中过于热烈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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