Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不允许iFrame显示页面_Javascript_Html_Iframe - Fatal编程技术网

Javascript 不允许iFrame显示页面

Javascript 不允许iFrame显示页面,javascript,html,iframe,Javascript,Html,Iframe,如何禁止人们在iframe中使用我的页面。是否可以只使用HTML或必须使用JavaScript/jQuery。使用iframe脚本的中断: <script type="text/javascript"> if (top.location != self.location) { top.location = self.location.href; } </script> if(top.location!=se

如何禁止人们在iframe中使用我的页面。是否可以只使用HTML或必须使用JavaScript/jQuery。

使用iframe脚本的中断:

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

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

有两种不同的方法来解决这个问题,我强烈建议您阅读,如果您有机会的话

前两个选项使用HTTP头,它不使用任何HTML、JavaScript/jQuery,但需要在web服务器上进行配置

  • 使用
    X-FRAME-OPTIONS
    标题

    • X-FRAME-OPTIONS:deny
      -帧内无渲染
    • X-FRAME-OPTIONS:sameorigin
      -如果原点不匹配,则不进行渲染
  • Content Security Policy
    标题允许您使用
    frame-procents
    指令 指定允许哪些来源将页面嵌入框架或iframe。这方面的下降趋势是,它没有提供一种强制执行全局策略的方法

    • Twitter的CSP
      内容安全策略示例:frame祖先*.Twitter.com
    • 以下是浏览器兼容性-
  • 编辑
    帧祖先
    实际上是

    建议的方法是在页面顶部使用以下代码片段。如果是iframed,它将隐藏页面内容,否则将显示内容

    <style> html {display:none;} </style>
    <script>
       if (self == top) {
           document.documentElement.style.display = 'block'; 
       } 
       else {
           top.location = self.location; 
       }
    </script>
    
    html{display:none;}
    if(self==顶部){
    document.documentElement.style.display='block';
    } 
    否则{
    top.location=self.location;
    }
    
    您可以使用,但当您在尝试编写framekiller或集成现有framekiller时遇到问题,提供特定错误/警告等时,本网站将为您提供帮助。