Js中的window.parent ,window.top,window.self 详解

来源:互联网 发布:端口聚合 编辑:程序博客网 时间:2024/05/17 15:19

在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法打开当前窗口的那个窗口。

window.self

功能:是对当前窗口自身的引用。它和window属性是等价的。

语法:window.self

注:window、self、window.self是等价的。

window.top

功能:返回顶层窗口,即浏览器窗口。

语法:window.top

注:如果窗口本身就是顶层窗口,top属性返回的是对自身的引用。

window.parent

功能:返回父窗口。

语法:window.parent

注:如果窗口本身是顶层窗口,parent属性返回的是对自身的引用。

在框架网页中,一般父窗口就是顶层窗口,但如果框架中还有框架,父窗口和顶层窗口就不一定相同了。

 

判断当前窗口是否在一个框架中:

<script type="text/JavaScript">
var b = window.top!=window.self;
document.write( "当前窗口是否在一个框架中:"+b );
</script>

你应当将框架视为窗口中的不同区域,框架是浏览器窗口中特定的部分。一个浏览器窗口可以根据你的需要分成任意多的框架,一个单个的框架也可以分成其它多个框架,即所谓的嵌套框架。


window.top.location

top, 表示是顶层页面, 因为页面之中可能嵌入了 frame 等子页面

Html代码 
  1. <html>  
  2. <head>  
  3. <script type="text/javascript">  
  4. function breakout()   
  5.   {   
  6.   if (window.top!=window.self)    
  7.     {   
  8.     window.top.location="/example/hdom/tryjs_breakout.htm"  
  9.     }   
  10.   }   
  11. </script>  
  12. </head>  
  13. <body>  
  14.   
  15. <input type="button" onclick="breakout()" value="跳出框架!">  
  16.   
  17. </body>  
  18. </html>    

 就是你的顶层网页页面的地址对象。
比如现在这个页面,它的location.href 就是http://zhidao.baidu.com/question/8493.html

而location, 是一个对象,它包括了上面说的那个地址,还有一些方法,
比如replace(替换地址) 等, 和属性 比如 href (地址)





0 0
原创粉丝点击