javascript 在浏览器选项卡关闭时调用 JS 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17975068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling JS functions on browser tab close
提问by isc
I need to call a JS function when user closing the browser tab. the problem is i want to happen this only when user closing the browser. no need to happen for page refresh, link navigation,form submit and back button press. i tried the below jquery code so far.
当用户关闭浏览器选项卡时,我需要调用一个 JS 函数。问题是我只想在用户关闭浏览器时发生这种情况。无需发生页面刷新、链接导航、表单提交和后退按钮按下。到目前为止,我尝试了以下 jquery 代码。
$(window).bind(
"beforeunload",
function() {
alert("don't close me");
return false;
}
)
$('form').submit(function() {
jQuery(window).unbind("beforeunload");
});
its not working. can anyone answer for this. is there any other js tools than jquery available for this?.
它不工作。任何人都可以回答这个问题。除了 jquery 之外,还有其他 js 工具可用于此吗?
And if i call my function "beforeunload" event, above message is coming. i don't want to show this message and my function has to be worked. i tried by giving e.preventDefault. but it's not calling my function also when i used that. can anybody suggest something. Thanks.
如果我调用我的函数“beforeunload”事件,就会出现上面的消息。我不想显示此消息,我的功能必须工作。我尝试通过提供 e.preventDefault。但是当我使用它时它也没有调用我的函数。任何人都可以提出一些建议。谢谢。
回答by mwcz
I agree with the comments that this is a bad practice, but I can't resist the call to attempt answering the question.
我同意这是一种不好的做法的评论,但我无法抗拒尝试回答这个问题的呼吁。
The only way I can think of to accomplish this is to use onbeforeunload
, which you're already doing. But you need a way of disabling that alert when someone navigates away from the page by some other means.
我能想到的唯一方法是使用onbeforeunload
你已经在做的。但是,当有人通过其他方式离开页面时,您需要一种禁用该警报的方法。
var show_close_alert = true;
$("a").bind("mouseup", function() {
show_close_alert = false;
});
$("form").bind("submit", function() {
show_close_alert = false;
});
$(window).bind("beforeunload", function() {
if (show_close_alert) {
return "Killing me won't bring her back...";
}
});
It's not foolproof, as in there are ways to close the browser without seeing the alert (like clicking a link, hitting Stop immediately, and then closing the browser), but it may be as close as you can get.
这不是万无一失的,因为有多种方法可以在不看到警报的情况下关闭浏览器(例如单击链接,立即点击停止,然后关闭浏览器),但它可能会尽可能接近。
Here's a fiddle.
这是一个小提琴。
But please... don't do this.
但是请...不要这样做。
回答by Jitendra Pancholi
onunload(or onbeforeunload) cannot redirect the user to another page. This is for security reasons.
onunload(或onbeforeunload)不能将用户重定向到另一个页面。这是出于安全原因。
If you want to show a prompt before the user leaves the page, use onbeforeunload:
如果要在用户离开页面之前显示提示,请使用 onbeforeunload:
window.onbeforeunload = function(){
return 'Are you sure you want to leave?';
};
Or with jQuery:
或者使用 jQuery:
$(window).bind('beforeunload', function(){
return 'Are you sure you want to leave?';
});
This will just ask the user if they want to leave the page or not, you cannot redirect them if they select to stay on the page. If they select to leave, the browser will go where they told it to go.
这只会询问用户是否要离开页面,如果他们选择留在页面上,则无法重定向他们。如果他们选择离开,浏览器将转到他们告诉它去的地方。
You can use onunload to do stuff before the page is unloaded, but you cannot redirect from there (Chrome 14+ blocks alerts inside onunload):
您可以在页面卸载之前使用 onunload 执行操作,但您无法从那里重定向(Chrome 14+ 会阻止 onunload 内的警报):
window.onunload = function() {
alert('Bye.');
}
Or with jQuery:
或者使用 jQuery:
$(window).unload(function(){
alert('Bye.');
});
回答by MaxPRafferty
JSFiddle uses the following to alert the user they are trying to close their modified fiddle without saving:
JSFiddle 使用以下内容来提醒用户他们试图在不保存的情况下关闭修改后的小提琴:
window.onbeforeunload = function(){
if (window.editorsModified){
return "You've modified your fiddle, reloading the page will reset all changes."
}
};
This works reliably (http://jsfiddle.net/MaxPRafferty/b6uEF/show/,Latest Chrome, FF, IE10 ), but will always prompt the user. You can't force the page open by returning false;
这工作可靠(http://jsfiddle.net/MaxPRafferty/b6uEF/show/,Latest Chrome, FF, IE10 ),但总是会提示用户。您不能通过返回 false 来强制打开页面;