在 iframe 頁面中執行 window.parent.document.getElementById("CloseWindows").onclick = function() {},IE 不執行,無報錯。
瀏覽器禁止不同源(協議/域名/端口任一不同)的文檔互相訪問 DOM。IE 對同源策略執行尤為嚴格,甚至阻止同源但信任區域不同的訪問。
document.domain 設置失效。強制邊緣模式:<meta http-equiv="X-UA-Compatible" content="IE=Edge">。document.domain = 'example.com';(僅限 IE/舊版 Edge)。跨域通訊首選 window.postMessage,安全且標準化:
// 父頁面
window.addEventListener('message', (e) => {
if (e.origin !== 'https://trusted-child.com') return;
if (e.data === 'close') document.getElementById('CloseWindows').click();
});
// iframe 頁面
window.parent.postMessage('close', 'https://parent.com');
iframe.src = 'javascript:parent.handleClose()' 等 Hack,但極不推薦。