1. 程式人生 > >使用iframe框架後的頁面跳轉時目標頁面變為iframe的子頁面的問題

使用iframe框架後的頁面跳轉時目標頁面變為iframe的子頁面的問題

開發時,為了增加一個“安全退出”的功能,遇到這樣的問題: 我在前端頁面使用了iframe標籤(位於 index.xml 裡面):
<div class="Conframe">
<iframe name="Conframe" id="Conframe"></iframe>
</div>
然後,在“安全退出”的按鈕那裡,我原來時這樣寫的:
<li><b class="tip"></b><a target="Conframe" href="logout.action">安全退出</a></li>
在struts.xml裡面的配置如下:
<!-- 安全登出 -->
<action name="logout"
class="logoutAction">
<result name="success" type="redirect">login.jsp</result>
</action>
結果,點選“安全退出”之後,跳轉出來的目標頁面(login.jsp)總是作為 index.xml 的 iframe 裡面的子頁面,而不是一整個完整頁面跳轉到 login.jsp. 改了很久,試了很多方法,最終解決的方法是把“安全退出”的那個標籤的“target”屬性改為“_parent"(即上面的第二段程式碼),如下:
<li><b class="tip"></b><a 
target="_parent" href="logout.action">安全退出</a></li>
這樣問題就完美解決。 總的來說,使用 iframe 時,在子頁面中的跳轉要十分細心,跳轉的目標頁面時作為子頁面還是作為整個頁面。從子頁面使整個頁面跳轉的方法有兩個: (1)在標籤中跳轉,設定標籤的 target 屬性為 _parent; (2)在 js 中使用 window.location.href 跳轉,讓父頁面跟著一起跳轉,即在window.location.href=url後面加上window.parent.location.href=url ;