<iframe> 除嵌入頁面外,還有兩大「黑科技」用法:IE6 遮罩層底墊、跨域寫 Cookie。
IE6 <select> 屬於系統級控件,z-index 永遠最高,任何 div 遮罩都蓋不住。
// 邏輯
1. 與彈層 div 同級插入 iframe
2. iframe z-index 略低於彈層,高於 select
3. iframe 尺寸鋪滿視口,監聽 resize/scroll 同步
4. filter:Alpha(opacity=0) 完全透明
// 代碼片段
var iframe = isIE6 ? '<iframe style="position:absolute;left:0;top:0;z-index:2000000;filter:Alpha(opacity=0);width:100%;height:' + docHeight + '" frameborder="0"></iframe>' : '';
$container.append(iframe).appendTo($body);
原理:iframe 同樣為系統級窗口,能壓制 select。
主域 a.com 嵌入 b.com 的 iframe,iframe 內頁面屬於 b.com 域,可自由讀寫 b.com Cookie。
a.com 頁面動態創建 <iframe src="https://b.com/proxy.html">(隱藏)。proxy.html 執行 document.cookie = "key=value; domain=.b.com; path=/"。parent.postMessage('done', 'https://a.com')。postMessage 傳遞 Token,由主域後端設定 Set-Cookie (SameSite=None; Secure)。iframe 是「頁面中頁面」的強隔離容器,善用其隔離性解決歷史難題,但需權衡性能、安全、第三方 Cookie 限制。