如何以編程方式觸發onbeforeunload和onunload事件?(不需要jquery)。我已經試過:Javascript:以編程方式觸發onbeforeunload/onunload事件
var event = new Event('onbeforeunload');
event.initEvent("onbeforeunload", true, true);
window.document.dispatchEvent(event);
如何以編程方式觸發onbeforeunload和onunload事件?(不需要jquery)。我已經試過:Javascript:以編程方式觸發onbeforeunload/onunload事件
var event = new Event('onbeforeunload');
event.initEvent("onbeforeunload", true, true);
window.document.dispatchEvent(event);
要麼使用
object.onunload=function(){myScript};
或addEventListener()方法:
object.addEventListener("unload", myScript);
觸發事件中使用 object.unload ();
鑑於我們有'window.onbeforeunload = function(){return 「你要離開」;}',如果我調用'window.onbeforeunload',不會彈出警告框,但如果我調用'window.location ='http:// someotherdomain.com''這將最終觸發onbeforeunload,我看到一個彈出的字符串「你要走了」。那麼有沒有一種方法可以模擬完全離開頁面,就像我們關閉一個窗口時發生的一樣?!(顯然我不是在尋找window.close()) – Alex
我認爲問題是關於以編程方式觸發瀏覽器確認onbeforeunload創建。這個答案並沒有真正做到。 –
請顯示您的代碼。 – Lance
http://stackoverflow.com/questions/3490331/is-it-possible-to-trigger-the-onbeforeunload-event-programmatically – Tushar