頁面嵌入 `
<script> // 1. _top:載入至最頂層視窗(穿透所有巢狀框架) window.open('login.aspx', '_top'); // 2. _parent:載入至父視窗(僅穿透一層) window.open('login.aspx', '_parent'); // 3. 直接修改頂層 location(最常用) window.top.location.href = 'login.aspx'; // 或 window.parent.location.href = 'login.aspx'; </script>
<!-- 直接跳至頂層 --> <a href="login.aspx" target="_top">登出</a> <!-- 跳至父層 --> <a href="login.aspx" target="_parent">返回上層</a>
<form action="login.aspx" method="post" target="_top"> <input type="submit" value="登出"> </form>
<script> // 適用:框架頁面有特殊瀏覽器設定,需回預設環境 window.parent.opener = null; // 切斷 opener 關聯 window.top.close(); // 關閉頂層視窗(需同源權限) window.open('index.aspx', '_blank'); // 開新視窗 </script>
注意:現代瀏覽器限制 window.close() 僅能關閉腳本開啟的視窗,跨域框架無法關閉頂層。
window.close()
parent/top
<script> window.addEventListener('message', function(e) { if (e.origin !== 'https://child.com') return; // 驗證來源 if (e.data.action === 'breakout') { window.location.href = e.data.url || 'login.aspx'; } }); </script> <script> function breakOut(url) { parent.postMessage({ action: 'breakout', url: url }, 'https://parent.com'); } // 使用 breakOut('https://parent.com/login.aspx'); </script>
sandbox
X-Frame-Options: DENY/SAMEORIGIN
Content-Security-Policy: frame-ancestors 'self'