1

I have an iframe on my site. I do not own the website in the iframe and can't edit the code.

When a user clicks a link inside the iframe the iframe redirects the top level url.

I want to detect when the page is about to be redirected and display a notice saying they are about to be redirected. I have tried using unload.function but it does not get triggered.

$(document).unload(function()
{
    alert(1);
});

Is it possible to execute js after the user has clicked the link in the iframe before the page is redirected?

Thanks

  • What you do is to put a div over the iframe and capture the events that way. When the user is ready to move one, simply move the div or erase it completely. – Dominique Fortin Sep 02 '20 at 20:20

1 Answers1

1

In the top level frame - i.e your code attach a beforeunload event to the window and then something like this...

window.onbeforeunload = function () {
  return "Are you sure you want to leave?";
};

This answer goes into detail about who will actually see your custom message - Is it possible to display a custom message in the beforeunload popup? - Most browsers ignore the string even though its in the spec

Ramakay
  • 2,919
  • 1
  • 5
  • 21
  • It’s true that the code for a `beforeunload` prompt is “something like that”, but it’s not that. :P – Ry- Sep 02 '20 at 20:22