[英]how to use window.onbeforeunload?
I have a small webpage. 我有一个小网页。 i need to activate my ajax function when browser is closed
关闭浏览器后,我需要激活我的ajax功能
I using below JavaScript for that in the <head>
我在
<head>
使用下面的JavaScript
<script language="JavaScript" type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
my ajax_function();
return "";
}
</script>
But this function works all in the Back button, Forward button, ALT+f5, CTL+f5, Submit etc... But i need this function only when browser is closed 但是此功能在“ 后退”按钮,“前进”按钮,ALT + f5,CTL + f5,“提交”等所有功能中均有效。但是我仅在关闭浏览器时才需要此功能
But i try like below in my webpage to block the events like Back button, Forward button, ALT+f5, CTL+f5, Submit etc... 但是我尝试在网页中像下面这样阻止诸如后退按钮,前进按钮,ALT + f5,CTL + f5,提交等事件。
<script type="text/javascript">
$(function () {
$('a').click(function(){window.onbeforeunload = null;});
$(window).keydown(function(event) {
if (event.keyCode == 116) { // User presses F5 to refresh
window.onbeforeunload = null;
}});
$('form').submit(function() {
window.onbeforeunload = null;
});
});
</script>
But this only for hyperlink, refresh, and submit. 但这仅适用于超链接,刷新和提交。
What i need is, window.onbeforeunload
function only works when browser is closed
我需要的是,
window.onbeforeunload
函数仅when browser is closed
JavaScript不会区分这些事件,它们都是由于用户离开页面而触发的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.