• js关闭被打开的iframe页面,并在新窗口打开


    示例代码:

    $(function(){
        console.log(window.location.href)
        if (self.frameElement && self.frameElement.tagName == "IFRAME") {
            // alert('在iframe中');
            window.open(window.location.href)
    
            parent.location.reload(true);
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
       });
    相关知识补充:
    判断页面是否被iframe有三种方法
    //方式一
    if (self.frameElement && self.frameElement.tagName == "IFRAME") {
      alert('在iframe中');
    }
    //方式二
    if (window.frames.length != parent.frames.length) {
      alert('在iframe中');
    }
    //方式三
    if (self != top) {
    alert('在iframe中');
    }

    禁止页面被别人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>
    但这种方式会禁止所有的页面的嵌入,那么本域名内的页面嵌入也是被禁止
    下面做到禁止除域名外的页面的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>
  • 相关阅读:
    [单链表]链表指针追赶问题
    二叉查找树与平衡二叉树
    二叉树的下一个结点
    fork进程函数总结
    《Effective C++》Item2:尽量以const,enum,inline替换#define
    Hash表的使用
    [数字]整数数字的算法
    算法题:找出整数数组中两个只出现一次的数字
    删除元素的操作
    [Reprinted] 使用Spring Data Redis操作Redis(一) 很全面
  • 原文地址:https://www.cnblogs.com/chenyingying0/p/16116419.html
Copyright © 2020-2023  润新知