2014-05-02 104 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>