在 chrome 和 firefox/IE 中试试这个:
var cancelPressed = false;
function redirect() {
//window.location = "http://www.google.com";
alert('hi!');
}
window.onbeforeunload = function() {
window.pressedTimer = setInterval("cancelPressed = true; clearInterval(window.pressedTimer);",3000);
window.onbeforeunload = function() {
if (!cancelPressed) {
window.unloadTimer = setTimeout('redirect()',500);
window.onbeforeunload = function() {clearTimeout(window.unloadTimer);};
return "Redirecting..";
} else {
return 'wups';
}
};
return 'first!';
};
在 FF/IE 中,刷新,在第一个提示上点击取消,等待大约六秒钟,然后尝试刷新。'wups' 将被触发。但是,在 Chrome 中,您可以随意等待,并且 cancelPressed 永远不会设置为 true。
你怎么看?