js

任何域名都无法嵌入iframe框架1:

<script type="text/JavaScript">
  if (window!=top) // 判断当前的window对象是否是top对象
  top.location.href = window.location.href; // 如果不是,将top对象的网址自动导向被嵌入网页的网址
</script>

任何域名都无法嵌入iframe框架2:

<script language="javascript"> 
if(top != self){ 
 location.href = "about:blank"; //也可设置为你自己的URL
} 
</script>

任何域名都无法嵌入iframe框架3:

if(window != window.top){
    window.top.location.href = correctURL;
}

js判断是否同域名:

if (top.location.host != window.location.host) {
  top.location.href = window.location.href;
}

js 判断顶层窗口跳转1:

function locationTop(){  
    if (top.location != self.location) {  
        top.location = self.location;  
        return false;         
    }  
    return true;  
}  
locationTop();  

js 判断顶层窗口跳转2:

if(window.top !== window.self){ window.top.location = window.location;}

js 判断顶层窗口跳转3:

<script type="text/javascript">
document.domain="qq.com";var _s_=new Date(),g_T={},siDomain="ctc.qzonestyle.gtimg.cn",g_iUin=499469859,g_iLoginUin=499469859;g_T.fwp=[_s_];document.namespaces&&document.namespaces.add&&(document.namespaces.add('qz', 'http://qzone.qq.com/'),document.namespaces.add('x', 'http://qzone.qq.com/'));var QZFL={};QZFL.event={};QZFL.event.getEvent=function(evt){var evt=window.event||evt,c,cnt;if(!evt&&window.Event){c=arguments.callee;cnt=0;while(c){if((evt=c.arguments[0])&&typeof(evt.srcElement)!="undefined"){break;}else if(cnt>9){break;}c=c.caller;++cnt;}}return evt;};QZFL.event.getTarget=function(evt){var e=QZFL.event.getEvent(evt);if(e){return e.srcElement||e.target;}else{return null;}};var QZFF_M_img_ribr=[];QZFL.media={reduceImgByRule:function(ew,eh,opts,cb){QZFF_M_img_ribr.push(QZFL.event.getTarget());},adjustImageSize:function(w,h,trueSrc,cb,errCallback){QZFF_M_img_ribr.push(QZFL.event.getTarget());},reduceImage:function(){QZFF_M_img_ribr.push(QZFL.event.getTarget());},getImageInfo:function(){QZFF_M_img_ribr.push(QZFL.event.getTarget());}};g_T.fwp[1] = new Date();</script>

js 判断顶层窗口跳转4:

<script language="JavaScript">
try{
  top.location.hostname;
  if (top.location.hostname != window.location.hostname) {
    top.location.href =window.location.href;
  }
}
catch(e){
  top.location.href = window.location.href;
}
</script>

解:

// 顶层窗口中放入代码  
var location = document.location;  
// 或者 var location = "";  

解:

<iframe src="你的页面地址" name="tv" marginwidth="0" marginheight="0" scrolling="No" noResize frameborder="0" id="tv" framespacing="0" width="580" height="550" VSPACE=-145 HSPACE=-385></iframe> 
<script language="javascript"> 
var location=""; 
var navigate=""; 
frames[0].location.href=""; 
</script>

判断页面是否被iframe三种方法:

//方式一 
if (self.frameElement && self.frameElement.tagName == "IFRAME") { 
  alert('在iframe中'); 
} 
//方式二 
if (window.frames.length != parent.frames.length) { 
  alert('在iframe中'); 
} 
//方式三 
if (self != top) { 
 alert('在iframe中'); 
}

header 控制

网站Sitemap的一些规则 :
使用 X-Frame-Options 有三个可选的值:
DENY:浏览器拒绝当前页面加载任何Frame页面
SAMEORIGIN:frame页面的地址只能为同源域名下的页面
ALLOW-FROM:允许frame加载的页面地址
PHP代码:

header('X-Frame-Options:Deny');
header('X-Frame-Options:SAMEORIGIN);

Nginx配置:

add_header X-Frame-Options SAMEORIGIN

Apache配置:

Header always append X-Frame-Options SAMEORIGIN

配置 X-FRAME-OPTIONS:

Apache

把下面这行添加到 'site' 的配置中:

Header always append X-Frame-Options SAMEORIGIN

nginx

把下面这行添加到 'http', 'server' 或者 'location',配置中

add_header X-Frame-Options SAMEORIGIN;

IIS

添加下面配置到 Web.config 文件中

  <system.webServer>
...

<httpProtocol>
  <customHeaders>
    <add name="X-Frame-Options" value="SAMEORIGIN" />
  </customHeaders>
</httpProtocol>

...
</system.webServer>

PHP方法

<?php header('X-Frame-Options:Deny'); ?>

.htaccess方法

在网站根目录下的.htaccess文件中中加一句:

Header append X-FRAME-OPTIONS "SAMEORIGIN"

文章参考:

JS防止网页被嵌入iframe框架的方法 - 365学习 - 365建站网
https://www.365jz.com/article/25060
点击劫持漏洞:使用X-Frame-Options 防止网页被Frame_打杂人的博客-CSDN博客_frame redirect
https://blog.csdn.net/aerchi/article/details/66974484
JavaScript JS利用location对象获取页面url、服务器地址、端口号、项目根路径和查询参 - 哔哩哔哩
https://www.bilibili.com/read/cv10436481
安全篇 - 前端大基础 - 博客园
https://www.cnblogs.com/still1/p/11008173.html
禁止网站被别人通过iframe引用嵌套套入
http://wangxinyang.cn/wxy/view.asp?id=240
JS判断页面是否被iframe及禁止页面被iframe - 微lin - 博客园
https://www.cnblogs.com/bella-lin/p/9266994.html
X-Frame-Options(如何防止点击劫持、设置页面是否能作为iframe嵌套)、iframe长轮询、优缺点(阻塞页面和唯一连接池、动态加载)和应用场景
https://blog.csdn.net/AIWWY/article/details/121240688