js:將當前窗口代替主窗口

任務:一個頁面裏包含兩個iframe(暫且叫left iframeright iframe),如今要求right iframe加載一個特定頁面時,當即將此頁面在主窗口顯示。
 
這裏設計3個頁面,index.htmlleft.htmlright.html。其中index中有兩個iframe(left iframeright iframe), left iframe顯示頁面爲left.htmlright iframe顯示爲right.html
Index.html:
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title >index </title>
</head>
< body >
< div id ="container" >
< iframe src ="left.htm" name ="leftFrame" width ="20%" height ="100%" align ="middle" scrolling ="no" />
< iframe src ="right.html" id ="platformFrame" name ="platformFrame" width ="80%" height ="100%" align ="middle" scrolling ="auto" />
</div>    
</body>
</html>
Left.html:
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title >index </title>
</head>
< body >
left
</body>
</html>
Right.html:
 
< html >
< head > </head>
< body >
right
</body>
< script type ="text/javascript" language ="javascript" >    
< ! --window.top:最頂層窗口     window.self:當前窗口 -- >
if (window.top!=window.self)    
{
  window.top.location='right.html'
}
</script>    
</html>
以上最主要的就是就是這段JavaScript
if (window.top!=window.self)
{
       window.top.location='right.html'
}
它的意思就是當right.html不爲頂層窗口時將它設置爲頂層窗口。
right.html沒有這段js時點擊index.html頁面,將以下顯示:
right.html有這段js時點擊index.html頁面,將以下顯示:
相關文章
相關標籤/搜索