OnBeforeUnload to redirect user

匆匆过客 提交于 2019-12-12 04:08:23

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6626940/onbeforeunload-to-redirect-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!