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