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); 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); Either use
object.onunload=function(){myScript}; or the addEventListener() method:
object.addEventListener("unload", myScript); to trigger the event use object.unload();
window.onbeforeunload=function(){return "you are leaving";} , if I call window.onbeforeunload, no alert box would pop up, but if I call window.location = 'http://someotherdomain.com' which will eventually trigger onbeforeunload, I see a pop up with the string "you are leaving". So is there a way to simulate leaving a page completely as it happens when we close a window?!(obviously I'm not looking for window.close())