javascript - 使用 javascript 或 Jquery 从 iFrame 重定向

标签 javascript jquery html

如果将网站加载到 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?

最佳答案

您要查找的内容称为 Framekiller

以下是该文章中建议的实现:

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

关于javascript - 使用 javascript 或 Jquery 从 iFrame 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11332247/

相关文章:

javascript - Html <img src=...> 有效但 JS 图像加载导致 CORS 错误

Javascript - 非稀疏数组中的数组索引问题

jquery - 找不到 'jquery' 的类型定义文件

html - 底部的 iframe 在页脚后生成空白

javascript - Regex Javascript(必须有 1 个数字和字符)

javascript - Mocha、should.js 和 Promise catch 回调

jquery - 如何在 vaadin 6 中创建自定义 vaadin 组件

javascript - Flash 消息 - jQuery

html - 单击图像时用于调用操作的_正确_ html 元素是哪个?

javascript - div宽度改变函数