OnBeforeUnload重定向用户 [英] OnBeforeUnload to redirect user

查看:116
本文介绍了OnBeforeUnload重定向用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的OnBeforeUnload事件中,它询问用户他们是要离开还是留在页面上。当他们点击停留在页面上时,我想让它重定向到同一个窗口中的另一个网页。有没有办法做到这一点,而不只是显示/隐藏div?我可以重写函数吗?

有趣的!



可能在Internet Explorer中:

  window.onbeforeunload = function )
{
window.onbeforeunload = null;
location.assign('http://example.com');
返回警告信息;
};

如果您这么说,Firefox会在等待答案的同时前往第二页,然后离开页面。



Chrome会一直进入第二页,这意味着用户无法离开页面。所以这可能是一个非常糟糕的主意。



如果用户正在关闭他们的窗口,但它仍然可以正常工作 - 但您无法确定这是否是用户想要的。


In my OnBeforeUnload event, it asked the user whether they want to leave the page or stay on it. When they click "stay on page", I want it to then redirect them to another webpage in the same window. Is there a way to do this, without just showing/hiding divs? Can I override the function?

解决方案

Interesting!

Possible in Internet Explorer:

window.onbeforeunload = function()
{
    window.onbeforeunload = null;
    location.assign('http://example.com');
    return "Warning message";
};

Firefox will go to the second page whilst waiting for your answer and then leave the page if you say so.

Chrome will always go to the second page, meaning the user cannot leave the page. So this might be a fantastically bad idea.

If the user is closing their window, it works alright though - but you can't tell if this is what the user wants.

这篇关于OnBeforeUnload重定向用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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