一些文档建议 document.open()
支持将 MIME 类型作为其第一个参数。例如:HTML DOM Open Method (Dottoro) .
我还有一本古老的 JavaScript 教科书,它声称您可以将 MIME 类型传递给 document.open()。
但我看过的大多数文档都另有说法:
这是早期 JavaScript 支持的参数,后来被删除了吗?
我在 DOM 规范中没有看到它:
- https://www.w3.org/TR/REC-DOM-Level-1/level-one-html.html#ID-1006298752
- https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-72161170
这只是为了我的兴趣;我没有该参数的具体用例。
最佳答案
Chrome
Chrome 不使用 type
参数。
V8Document.openMethod()
方法检查 document.open(...)
参数的有效性,然后调用 v8Document.open1Method()
或 v8Document.open2Method()
。 v8Document.open2Method()
甚至不读取它提供的第一个 (type
) 参数。 v8Document.open1Method()
会读取它,如果未定义则将其设置为默认值 "text/html"
。然后它会将 type
值传递给 Document.open()
方法,但从那里它会被忽略。
火狐
Firefox 使用type
参数,但唯一接受的非默认值是"text/plain"
。
如果缺少参数,nsHTMLDocument::Open()
方法将 type
设置为 "text/html"
,然后调用另一个重载.重载将除 "text/html"
之外的所有 type
值转换为 "text/plain"
,然后将该内容类型应用于文档。
检测
.contentType
属性可以告诉我们我们拥有的文档
的类型。我们不能用它来提前进行特征检测,但我们可以用它来检查文档实际打开的类型,并相应地修改我们的输出。例如:
setTimeout(function() {
document.open('text/plain');
if (document.contentType == 'text/plain') {
document.write("I'm text/plain! :-D");
} else if (document.contentType == 'text/html') {
document.write("I'm <code>text/html</code>. :-(");
} else {
document.write("I'm confused! Also: " + document.contentType);
}
document.close();
});
关于javascript - JavaScript 的 document.open 是否支持 MIME 类型参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39233344/