首页 > Web开发 > 详细

js实现的防止别人嵌套自己的页面

时间:2017-03-14 11:57:52      阅读:179      评论:0      收藏:0      [点我收藏+]
我们使用iframe来嵌入页面时。

子窗口可以重写父窗口的location.href,

但是注意这里子窗口无法读取而只能重写location.href所以要求前提是您控制两个域名,知 道当前父窗口的location.href是什么并写在子窗口内,这样通过parent.location.href = "已知的父窗口的href"+"#"+hash。这样父窗口只有hash改变也不会重载。
 
所以为了防止别人iframe你的页面,可以像下面这样:
      
     try{
            if(window.location.href != parent.window.location.href){parent.window.location.href = window.location.href}
        }catch(e){
            parent.window.location.href = window.location.href    ··
        }

 

 
 
上面这个可以变成下面这个,(就是说,只要是不同域的,都直接跳到为iframe的地址)
        try{
            parent.window.location.href
        }catch(e){
            parent.window.location.href = window.location.href
        }

 

js实现的防止别人嵌套自己的页面

原文:http://www.cnblogs.com/jczxq/p/6547608.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!