最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Redirect from iFrame using javascript or Jquery - Stack Overflow

programmeradmin4浏览0评论

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 = ""; 
}
</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"></iframe>
</body>
</html>

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

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."; 
}
</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."></iframe>
</body>
</html>

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

Share Improve this question edited Jul 4, 2012 at 16:45 Patriotec asked Jul 4, 2012 at 15:49 PatriotecPatriotec 1,1444 gold badges23 silver badges43 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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>

This should work:-

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

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

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

发布评论

评论列表(0)

  1. 暂无评论