Javascript:以编程方式触发 onbeforeunload/onunload 事件

2024-04-09

如何以编程方式触发 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'))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Javascript:以编程方式触发 onbeforeunload/onunload 事件 的相关文章

随机推荐