How to Prevent Iframe from Redirecting Top-Level Window

How to prevent IFRAME from redirecting top-level window

Try using the onbeforeunload property, which will let the user choose whether he wants to navigate away from the page.

Example: https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload

In HTML5 you can use sandbox property. Please see Pankrat's answer below.
http://www.html5rocks.com/en/tutorials/security/sandboxed-iframes/

Stop an embedded iframe from redirecting

As your iframe is obviously served by another domain, standard solutions cannot be used. You can't read or modify the iframe nor its document.

But you can put an invisible div over it like this : http://jsfiddle.net/dystroy/Afg3K/

<div id=a></div>

#a{
position:fixed;
top:0;
left:0;
width:224px;
height:429px;
}​

But most of the times, those things are forbidden by the iframe inclusion contract. Look at your agreement before you do it.

Preventing iframe top-level redirect

Actually since you already using sandbox="" in your IFRAME, it already prevents it from changing top-level location. You can safely remove your onbeforeunload event handler.

Note: sandbox="" does not work in IE9 and below.



Related Topics



Leave a reply



Submit