Organizational Research By

Surprising Reserch Topic

how to prevent iframe from redirecting top level window

how to prevent iframe from redirecting top level window  using -'javascript,html,iframe'

Some websites have code to "break out" of IFRAME enclosures, meaning that if a page A is loaded as an IFRAME inside an parent page P some Javascript in A redirects the outer window to A.

Typically this Javascript looks something like this:

<script type="text/javascript">
  if (top.location.href != self.location.href)
     top.location.href = self.location.href;

My question is: As the author of the parent page P and not being the author of the inner page A, how can I prevent A from doing this break-out?

P.S. It seems to me like it ought to be a cross-site security violation, but it isn't.

asked Sep 16, 2015 by yogeshplv
0 votes

Related Hot Questions

8 Answers

0 votes

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


In HTML5 you can use sandbox property. Please see Pankrat's answer below.

answered Sep 16, 2015 by abhi
0 votes

With HTML5 the iframe sandbox attribute was added. At the time of writing this works on Chrome, Safari, Firefox and recent versions of IE and Opera but does pretty much what you want:

If you want to allow top-level redirects specify sandbox="allow-top-navigation".

answered Sep 16, 2015 by sameer rathore
0 votes

After reading the spec. I found the sandbox property.

You can set sandbox="", which prevents the iframe from redirecting. That being said it won't redirect the iframe either. You will lose the click essentially.

Example here:
Example without sandbox:

answered Sep 16, 2015 by sachin valanju
0 votes

I know it has been a long time since question was done but here is my improved version it will wait 500ms for any subsequent call only when the iframe is loaded.

and onload="frameLoad()" and onreadystatechange="frameLoad();" must be added to the frame or iframe.

answered Sep 16, 2015 by girisha
0 votes

I use sandbox="..."

  • allow-forms allows form submission
  • allow-popups allows popups
  • allow-pointer-lock allows pointer lock
  • allow-same-origin allows the document to maintain its origin
  • allow-scripts allows JavaScript execution, and also allows features to trigger automatically
  • allow-top-navigation allows the document to break out of the frame by navigating the top-level window

Top navigation is what you want to prevent, so leave that out and it will not be allowed. Anything left out will be blocked


answered Sep 16, 2015 by mtabakade
0 votes

Since the page you load inside the iframe can execute the "break out" code with a setInterval, onbeforeunload might not be that practical, since it could flud the user with 'Are you sure you want to leave?' dialogs.

There is also the iframe security attribute which only works on IE & Opera


answered Sep 16, 2015 by akhilesh
0 votes

In my case I want the user to visit the inner page so that server will see their ip as a visitor. If I use the php proxy technique I think that the inner page will see my server ip as a visitor so it is not good. The only solution I got so far is wilth onbeforeunload. Put this on your page:

This works both in firefox and ie, thats what I tested for. you will find versions using something like evt.return(whatever) crap... that doesn't work in firefox.

answered Sep 16, 2015 by nimisha.jagtap