2014-05-02 98 views
1

如何禁用用户在iframe中使用我的页面。是否有可能使用HTML或必须使用JavaScript/jQuery。不允许显示页面的iframe

+0

您可以通过使用[Framekiller(http://en.wikipedia.org/wiki/Framekiller),但这个网站是为了帮助你,你在你尝试写一个framekiller或整合后的问题现有的,提供具体的错误/警告等。 – ajax333221

回答

2

有几种不同的方式来处理这个问题,进行了详细的Busting Frame Busting paper我强烈建议,如果你有机会,你阅读说明。

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

  1. 使用X-FRAME-OPTIONS标题。

    • X-FRAME-OPTIONS: deny - 没有渲染,如果原产地不一致
  2. Content-Security-Policy头允许您使用frame-ancestors指令 来指定哪些起源允许嵌入 - 框架

  3. X-FRAME-OPTIONS: sameorigin内没有渲染页面放入框架或iframe中。对此的下降是它没有提供一种强制执行宽泛政策的方法。

EDITframe-ancestors实际上是Content Security Policy Level 2部分的量,compatibility can be seen here

所建议的方法正在使用以下内容在页面顶部添加一小段代码。它会隐藏你的网页的内容,如果它是iframed,并会显示其他内容。

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

使用挣脱出来iframe的脚本:

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