DIV CSS 佈局教程網

JS 如何跳出頁面的 frameset 框架
編輯:JavaScript綜合知識     

許多老舊網站仍採用框架(frame/frameset)結構。在實際操作時,我們常會遇到需要「跳出框架」的情境,例如點擊「登出」後要回到獨立的登入畫面,而不希望它繼續被包在原本的框架裡。本文整理了几種常見的 JavaScript 跳出框架寫法。

一、透過程式碼跳出框架

以下幾種寫法都能達到跳出框架的效果:

1. <script language=javascript>window.open('Login.aspx','_top')</script>
2. <script language=javascript>window.open('Login.aspx','_parent')</script>
3. <script language=javascript>window.parent.location.href='login.aspx'</script>

其中 _top 代表把視窗最外層整個導向新頁面,_parent 則是跳出目前的框架層級。

還有一種更徹底的寫法,會先關閉原框架視窗,再重新開啟一個全新視窗:

4. Response.Write("<script>window.parent.opener=null;window.top.close();</script>")
   Response.Write("<script>window.open('index.aspx','');</script>")

這種做法特別適合那些對瀏覽器做了特殊設定、回到登入畫面時又想恢復預設狀態的情況。

二、透過連結屬性跳出框架

如果只是單純的連結跳轉,那更簡單——只要在 <a> 標籤加上 target="_top" 屬性即可:

<a href="login.aspx" target="_top">登出</a>

點擊後,瀏覽器會直接把最外層視窗導向目標頁面,順利擺脫框架的束縛。

小結

要跳出 frameset,核心觀念就是操作 window.top、window.parent 或善用 target="_top"。依你的實際需求(是要重新導向、關閉重開,還是單純連結)挑選合適的寫法即可。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved