我正在尝试重定向onbeforeunload,但根本没有成功。这是我到目前为止所知道的:
window.location.href似乎不起作用。
window.onbeforeunload = function(e) {
window.location.href = ("http://www.google.com");
return "Are you sure you want to leave?";
};这个是持久的,它重定向用户是否提示留下或离开。我只想在用户选择离开时重定向。
var stayonthis = true;
var a;
function load() {
window.onbeforeunload = function(e) {
if(stayonthis){
a = setTimeout('window.location.href="http://google.com";',100);
stayonthis = false;
return "Do you really want to leave now?";
}
else {
clearTimeout(a);
}
};
window.onunload = function(e) {
clearTimeout(a);
};
}
window.onload = load;我能想到的最接近的东西是这个,但它会在新窗口中打开链接。我只想在同一窗口中打开链接。
window.onbeforeunload = function(){
window.open("http://www.google.com");//Setting parameters to _self or _parent still opens in new window.
return "Are you sure you want to leave?";
}我正在尝试重定向到一个内部url,如果这很重要的话。
非常感谢您的建议。谢谢。
发布于 2014-03-24 06:42:29
Onbeforeunload旨在提示用户一条消息,如果您认为他们可能需要留在页面上。您不能基于这些响应或重定向或任何性质执行操作,如果他们单击stay,则他们会留下来,如果他们单击leave,则不会留下任何其他内容。
https://stackoverflow.com/questions/22598046
复制相似问题