iframe在卸载事件之前 [英] iframe just before unload event

查看:691
本文介绍了iframe在卸载事件之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在努力完成像这样的事情

  $ iframe = $('iframe'); 
$ iframe.beforeunload(function(){
debugger;
});

OR

  $ iframe = $('iframe'); 
$ iframe.unload(function(){
debugger;
});

我甚至尝试将它绑定到iframe窗口本身,但没有任何运气

  $ iframe = $('iframe'); 
$ iframe [0] .contentWindow.onunload = function(){
debugger;
};

这些事件处理程序都不会触发我



,我很困惑为什么。重新加载iframe我从iframe外部使用 .reload(),也许我需要使用不同的方法?

解决方案

想通了!我不知道 contentWindow 在重新加载后会丢失它的引用。

  $ iframe.load(function(){
$ iframe [0] .contentWindow.onbeforeunload = function(){
debugger;
};
});


I need to capture an event which should be triggered just before the content of the iframe disappears.

I have been trying to accomplish something like this

$iframe = $('iframe');
$iframe.beforeunload(function () {
  debugger;
});

OR

$iframe = $('iframe');
$iframe.unload(function () {
  debugger;
});

I have even tried binding it to the iframe window itself without any luck

$iframe = $('iframe');
$iframe[0].contentWindow.onunload = function () {
  debugger;
};

None of these eventhandlers actually trigger for me

and I am quite confused why. To reload the iframe I use .reload() from outside the iframe and from within, maybe I need to use a different method?

解决方案

Figured it out! I didn't know the contentWindow looses its reference after a reload.

$iframe.load(function () {
  $iframe[0].contentWindow.onbeforeunload = function () {
    debugger;
  };
});

这篇关于iframe在卸载事件之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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