簡體   English   中英

如果自我===頂部或google.com

[英]if self === top or google.com

<script type="text/javascript">
         if (self === top) {
          var antiClickjack = document.getElementById("antiClickjack");
          antiClickjack.parentNode.removeChild(antiClickjack);
         } else {
          top.location = self.location;
         }
      </script>

我有這個代碼,不允許iframe但我想只允許來自google.com的iframe我該怎么做?

我認為偽代碼將是if (self === top) or google.com但不知道如何在javascript中執行此操作

您可以使用top.location.host檢查主機名,如下所示:

if (self === top || top.location.host === 'google.com') {
  //good to go
}

但如果主機名不同,則無法更改頂部的位置:

if (self != top) {
  document.write('cross domain');
}
if (self.location.hostname !== top.location.hostname) {
  top.location.href = self.location.href; //throws exception
}

拋出:

未捕獲的SecurityError:阻止具有原點“ http://mbr.domain2.com ”的幀訪問具有原點“ http://mbr.domain.com ”的幀。 協議,域和端口必須匹配。

所以,我不認為這會做你想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM