简体   繁体   English

onbeforeunload 不/不正确工作在 Javascript

[英]onbeforeunload not/not correct working in Javascript

I'm trying to use the onbeforeunload or onunload event or even in a:我正在尝试使用 onbeforeunload 或 onunload 事件,甚至在:

window.addEventListener('beforeunload', function(){alert("3");}, false); 

but it doesn't work, neither on Chrome nor on Firefox, the only method that works is the code below, but the problem is that it works at the start of loading and not when the page being closed, I spent more than 24 hours just with that, and I can't get it to work.但它不起作用,无论是在 Chrome 上还是在 Firefox 上,唯一有效的方法是下面的代码,但问题是它在加载开始时有效,而不是在页面关闭时有效,我花了超过 24 小时就这样,我无法让它工作。

window.onbeforeunload  = closefct();
function closefct(e){
alert("5");
console.log("5");
return null
}

I'm trying to use the onbeforeunload or onunload event or even in a:我正在尝试使用 onbeforeunload 或 onunload 事件,甚至在:

window.addEventListener('beforeunload', function(){alert("3");}, false); 

but it doesn't work, neither on Chrome nor on Firefox, the only method that works is the code below, but the problem is that it works at the start of loading and not when the page being closed, I spent more than 24 hours just with that, and I can't get it to work.但它不起作用,无论是在 Chrome 还是 Firefox 上,唯一有效的方法是下面的代码,但问题是它在加载开始时起作用,而不是在页面关闭时起作用,我花了超过 24 小时就这样,我无法让它工作。

window.onbeforeunload  = closefct();
function closefct(e){
alert("5");
console.log("5");
return null
}

alert will be ignored by the browser. alert将被浏览器忽略。 Use console.log to check if it is working properly.使用 console.log 检查它是否正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM