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

    就是这么完美~

    码农随笔防失忆,只为记录风雨路上的脚丫印印~
  • 相关阅读:
    win10去除桌面快捷方式小箭头
    java创建线程的几种方式
    Spring知识点总结
    Myeclipse运行报错:an out of memory error has occurred的解决方法
    vue-cli的使用
    3种jQuery弹出大图效果
    Weixin API -- 微信js接口
    setTimeOut传参数
    PHP的八种数据类型
    99%的人都理解错了HTTP中GET与POST的区别
  • 原文地址:https://www.cnblogs.com/bella-lin/p/9266994.html
Copyright © 2020-2023  润新知