2

如果将网站加载到 iframe 中,我需要在该 iFrame 的子页面上放置什么代码才能脱离 iFrame 并将该页面加载为顶部文档或重定向到该页面

刚刚找到代码

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

更好的代码:

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

下面的代码示例:

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

我需要在(在本例中)www.blah.com 上放置什么 JQuery 或 javascript,以便它脱离 iframe 并直接向用户显示 www.blah.com?

4

4 回答 4

2

这应该工作: -

<script type="text/javascript">
  if (window!=top){top.location.href=location.href;}
</script>
于 2012-07-04T15:51:19.130 回答
2

您正在寻找的东西称为Framekiller

这是该文章中建议的实现:

<style> html{display : none ; } </style>
<script>
   if( self == top ) {
       document.documentElement.style.display = 'block' ; 
   } else {
       top.location = self.location ; 
   }
</script>
于 2012-07-04T15:55:02.677 回答
0
window.top.location.href = "http://blah.com/";

如此处所述(有更完整的解释): Redirect parent window from an iframe action

于 2012-07-04T15:53:16.880 回答
0

如果您使用链接设置 target="_top" 属性可能会完成这项工作。

于 2012-07-04T15:55:38.393 回答