首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

IE7 特有的 JavaScript 動態腳本注入失效排查指南

     編輯:程式設計綜合問答

問題代碼

this.r.document.writeln("<script type=\"text/javascript\">");
this.r.document.writeln(" setTimeout('parent.BindListMenu();',5000);");
this.r.document.writeln("</script>");

IE6/8 正常,IE7 不執行。

可能原因

  1. 字符串形式 setTimeout:IE7 對 setTimeout('code()', ...) 的 eval 執行上下文有限制,建議改用函數引用:
    setTimeout(parent.BindListMenu, 5000);
  2. document.write 時機:若頁面已加載完成,document.write 會隱式 document.open() 清空文檔。確保在頁面解析階段調用,或改用 DOM 方法:
    var s = document.createElement('script'); s.text = "parent.BindListMenu();"; document.head.appendChild(s);
  3. 跨框架域限制:parent.BindListMenu 要求同源策略允許。IE7 對 document.domain 設置更嚴格,確保父子頁面同域且均設置 document.domain = 'base.domain'。
  4. 腳本注入未生效:F12 開發者工具 → Script 標籤 → 檢查動態腳本是否真正插入 DOM。

調試建議

console.log('Injecting script...');
setTimeout(function() {
    console.log('Timeout fired, calling BindListMenu');
    if (typeof parent.BindListMenu === 'function') {
        parent.BindListMenu();
    } else {
        console.error('BindListMenu not found on parent');
    }
}, 5000);

現代替代

棄用 document.write,改用 fetch/import() 動態加載模塊,或使用 postMessage 跨框架通信。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved