2

If a website is loaded into an iframe, what code do I need to put on the child page of that iFrame to break out of the iFrame and load that page as the top document or reidrect to that page

Just found the code

<script>
if(window.top !== window.self){
window.top.location.href = "http://www.blah.com"; 
}
</script>

Even better code:

<style> html{display : none ; } </style>
<script>
   if( self == top ) {
       document.documentElement.style.display = 'block' ; 
   } else {
       top.location = self.location ; 
   }
</script>

Code Example below:

<!DOCTYPE html>
<html lang="en">
<head>
blah
</head>
<body>
<iframe src="www.blah.com"></iframe>
</body>
</html>

What JQuery or javascript do I need to put on (in this example) www.blah.com so it breaks out of the iframe and www.blah.com is directly shown to the user?

Patriotec
  • 1,104
  • 4
  • 22
  • 43

4 Answers4

2

This should work:-

<script type="text/javascript">
  if (window!=top){top.location.href=location.href;}
</script>
Purpletoucan
  • 6,472
  • 2
  • 21
  • 28
2

What you're looking for is called a Framekiller

Here's the suggested implementation from that article:

<style> html{display : none ; } </style>
<script>
   if( self == top ) {
       document.documentElement.style.display = 'block' ; 
   } else {
       top.location = self.location ; 
   }
</script>
Tom Chandler
  • 642
  • 3
  • 9
0
window.top.location.href = "http://blah.com/";

As mentioned here (with a fuller explanation): Redirect parent window from an iframe action

Community
  • 1
  • 1
Edd Slipszenko
  • 396
  • 3
  • 17
0

If you are using a link setting target="_top" attribute probably would do the job.

user1255409
  • 123
  • 1
  • 8
  • How would that bust out of an iframe? That would require clicking a link on the iframe's child page. Thank you, but that's not what I'm looking for – Patriotec Jul 04 '12 at 16:03