Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iframe just before unload event

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?

like image 378
Max Avatar asked Jun 08 '15 20:06

Max


People also ask

What triggers unload event?

onunload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.). Note: The onunload event is also triggered when a user reloads the page (and the onload event).

What is the difference between Onbeforeunload and Onunload?

onbeforeunload Below are my findings on the iPad; Using window. onunload , I am able to get an alert when user navigates to a different page from myPage. html (either by clicking on some link or doing a Google search while on myPage.


1 Answers

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

$iframe.load(function () {
  $iframe[0].contentWindow.onbeforeunload = function () {
    debugger;
  };
});
like image 71
Max Avatar answered Oct 21 '22 10:10

Max