[英]how to redirect when user closes window or tab
Is there a way to redirect the window or tab when a user closes it? 用户关闭时,是否可以重定向窗口或标签? This does not work (in jQuery):
这不起作用(在jQuery中):
$(window).bind('beforeunload', function() {
window.location = 'anotherpage.html'
}
Is it even possible? 可能吗? I just want to redirect a user to another page when they close it.
我只想在用户关闭页面时将其重定向到另一个页面。
No, the only thing you can do is alert the user they are about to close your page and ask them if they would like to remain or confirm to leave. 不,您唯一可以做的就是提醒用户他们即将关闭您的页面,并询问他们是否愿意留下或确认离开。
This is very common on pages which require you to save before exiting, like an online exam or survey for example. 这在要求您退出之前先保存的页面上非常普遍,例如在线考试或调查。
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
您无法转到新的网页,但是如果需要做的是打扫房间(例如向服务器发出ajax调用,清除一些cookie等),则可以直接在onbeforeunload处理程序上执行此操作,而无需提示用户,无需重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.