How can I programmatically trigger onbeforeunload and onunload events?(No jquery please). I've tried:
var event = new Event('onbeforeunload'); event.initEvent("onbeforeunload", true, true); window.document.dispatchEvent(event);
window.dispatchEvent(new Event('beforeunload'))
I think this would be the best way in 2020. In case anyone finds this.
window.addEventListener('beforeunload',()=>{console.log("beforeunload triggered")})
window.dispatchEvent(new Event('beforeunload'))
Either use
object.onunload=function(){myScript};
or the addEventListener() method:
object.addEventListener("unload", myScript);
to trigger the event use object.unload();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.