A-A+

js top.location判断自身是否是顶级窗口

2016年02月16日 编程技术 暂无评论

如果要在js中来判断自身是否是顶级窗口我们可直接利用top.location!=self.location来加以操作,具体看下面代码

顶级窗口的地址:top.location

本窗口的地址:self.location

代码如下:

  1. if(top.location!=self.location){  
  2. top.location = self.location;  
  3. }  

具体实现程序,代码如下:

  1. if (window.top !== window.self) { // are you trying to put self in an iframe?  
  2.   try {  
  3.     if (window.top.location.host) { // this is illegal to access unless you share a non-spoofable document domain  
  4.       // fun times  
  5.     } else {  
  6.       bust(); // chrome executes this  
  7.     }  
  8.   } catch (ex) {  
  9.     bust(); // everyone executes this  
  10.   }  
  11. }  
标签:

给我留言