[英]Javascript: Programmatically trigger onbeforeunload/onunload event
如何以編程方式觸發 onbeforeunload 和 onunload 事件?(請不要使用 jquery)。 我試過了:
var event = new Event('onbeforeunload'); event.initEvent("onbeforeunload", true, true); window.document.dispatchEvent(event);
window.dispatchEvent(new Event('beforeunload'))
我認為這將是 2020 年最好的方式。萬一有人發現了這個。
window.addEventListener('beforeunload',()=>{console.log("beforeunload triggered")})
window.dispatchEvent(new Event('beforeunload'))
要么使用
object.onunload=function(){myScript};
或 addEventListener() 方法:
object.addEventListener("unload", myScript);
觸發事件使用 object.unload();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.