文档.querySelector对于标准模式iframe在quirks模式父模式下运行是未定义的

document.querySelector is undefined for standards mode iframe running in quirks mode parent

本文关键字:模式 quirks 运行 未定义 iframe 于标准 标准 文档 querySelector      更新时间:2023-09-26

在使用书签工具运行IE 10时遇到此问题。当我在一个以怪癖模式运行的页面上运行书签时,我试图使用文档。querySelector,文档。querySelector未定义

试图解决这个问题,当我检测到文档。documentMode为5 (quirks模式),我创建了一个iframe,并将页面的内容复制到该iframe中,使其处于标准模式。我验证iframe中的文档是否处于标准模式(文档。documentMode是8 (ie8标准模式)但是文档。querySelector仍然未定义。我认为documentMode必须至少为9才能支持querySelector。我不知道为什么documentMode是8而不是10,因为我在IE 10上运行。

if(goog.userAgent.IE && document.documentMode <= 5) {
    // strip out any scripts from the body
    s = document.body.innerHTML.replace(/<script'b[^<]*(?:(?!<'/script>)<[^<]*)*<'/script>/gi, '');
    frame = goog.dom.iframe.createBlank(goog.dom.getDomHelper());
    frame.scrolling = "no";
    frame.allowTransparency = true;
    frame.style.visibility = 'hidden';
    document.body.appendChild(frame);
    goog.dom.iframe.writeContent(frame, '<!doctype html>'n<html><head><meta http-equiv="X-UA-Compatible" content="IE=edge"></head><body>' + s + '</body></html>');
    doc = goog.dom.getFrameContentDocument(frame);
    alert(doc.documentMode); // 8 - IE 8 standards mode
    alert(doc.querySelectorAll); // null
}

我使用documentMode而不是简单地检查(if(document.querySelector))来帮助我进一步调试这个问题