zoukankan      html  css  js  c++  java
  • JS判断页面是否被iframe及禁止页面被iframe

    1、判断页面是否被iframe有三种方法

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

    2、禁止页面被别人iframe了

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

    --注: 这种做法虽简单,但如果对方用如下招数很容易就被破解了

    <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>

    当然,万能的js依旧设计了应对招数

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

    当然,当然,这个也不是完美的奥,这种方式会禁止所有的页面的嵌入,那么本域名内的页面嵌入也是被禁止呢,嘤嘤~别着急,JS say no~ no~ no~

    我们依旧有办法可以做到禁止除域名外的页面的iframe

    <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>

    就是这么完美~

    码农随笔防失忆,只为记录风雨路上的脚丫印印~
  • 相关阅读:
    Html 回顾
    Parallel 并行编程
    Task---常用的多线程(基于多线程线程)
    Threadpool 可以对线程加以管理的封装
    AsyncThreads---异步多线程
    Abstract 封装,继承,多态
    IO&&Serize 利用线程Thread.Sleep实现"自动输出"
    Ling && Lambda
    Delegate&&Event
    Delegate &&Lambda
  • 原文地址:https://www.cnblogs.com/bella-lin/p/9266994.html
Copyright © 2011-2022 走看看