【发布时间】:2013-10-09 10:39:30
【问题描述】:
在使用小书签运行 IE 10 时遇到这种情况。当我针对以 quirks 模式运行的页面运行小书签并尝试使用 document.querySelector 时,document.querySelector 未定义。
为了解决这个问题,当我检测到 document.documentMode 为 5(怪癖模式)时,我创建了一个 iframe 并将页面内容复制到该 iframe 中以将其置于标准模式。我验证 iframe 中的文档处于标准模式(document.documentMode 为 8 - IE 8 标准模式),但 document.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
}
【问题讨论】:
-
为什么不使用特征检测来检测
querySelectorAll()而不是documentMode编号? -
我使用 documentMode 而不是简单地检查 (if(document.querySelector)) 来帮助我进一步调试这个问题
标签: javascript internet-explorer-10 quirks-mode selectors-api