onbeforeunload 在 IE 下的Bug
在页面需要离开或者刷新的时候, 需要注册事件, 我们都是用onbeforeunload 事件去检测和阻止,
但是, 现在发现一个诡异的问题, 如果在IE的a 标签里面写上 href="javascript:void(0)", 这个onbeforeunload事件还是将被触发。
查了一下MSDN, 上面的阐述是onbeforunload会有15种情况, 其实anchor.click不是我们想要的。
- Close the current window.
- Navigate to another location by entering a new address or selecting a Favorite.
- Click an anchor that refers to another document.
- Invoke the anchor.click method.
- Invoke the document.write method.
- Invoke the document.close method.
- Invoke the window.close method.
- Invoke the window.navigate or NavigateAndFind method.
- Invoke the location.replace method.
- Invoke the location.reload method.
- Specify a new value for the location.href property.
- Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.
- Invoke the window.open method, providing the possible value _self for the window name.
- Invoke the document.open method.
- Click the Back, Forward, Refresh, or Home button.
我们解决这个问题的方法是, 如果是IE的情况, 在onclick时间中注入return false, 可以解决这个问题, 代码如下
<body>
<a href="javascript:void(0)" id="ttt">sdf</a>
</body>
<script language="javascript">
se.bindEvent(window, "beforeunload", function(){
alert("ttttt");
});
se.bindEvent(se.id("ttt"), "click", function(e){
e.returnValue =false;
returnfalse;
});
</script>

浙公网安备 33010602011771号