Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  106] [ 6]  / answers: 1 / hits: 33995  / 9 Years ago, mon, june 8, 2015, 12:00:00

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?


More From » jquery

 Answers
68

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



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

[#66280] Friday, June 5, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
makenatiffanic

Total Points: 412
Total Questions: 106
Total Answers: 90

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
;