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?

4 Answers 4

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>
Sign up to request clarification or add additional context in comments.

Comments

2

This should work:-

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

Comments

0
window.top.location.href = "http://blah.com/";

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

Comments

0

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.