首页 > 代码库 > js实现的防止别人嵌套自己的页面

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

我们使用iframe来嵌入页面时。

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

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

 

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

 

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