Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
828 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...