Javascript:以编程方式触发onb​​eforeunload / onunload事件 [英] Javascript: Programmatically trigger onbeforeunload/onunload event

查看:132
本文介绍了Javascript:以编程方式触发onb​​eforeunload / onunload事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何以编程方式触发onb​​eforeunload和onunload事件(请不要jquery)。我试过:

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);

推荐答案

使用

object.onunload=function(){myScript};

或addEventListener()方法:

or the addEventListener() method:

object.addEventListener("unload", myScript);

触发事件使用
object.unload();

to trigger the event use object.unload();

这篇关于Javascript:以编程方式触发onb​​eforeunload / onunload事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆