javascript - JavaScript 的 document.open 是否支持 MIME 类型参数?

标签 javascript dom document

一些文档建议 document.open() 支持将 MIME 类型作为其第一个参数。例如:HTML DOM Open Method (Dottoro) .

我还有一本古老的 JavaScript 教科书,它声称您可以将 MIME 类型传递给 document.open()。 但我看过的大多数文档都另有说法:

这是早期 JavaScript 支持的参数,后来被删除了吗?

我在 DOM 规范中没有看到它:

这只是为了我的兴趣;我没有该参数的具体用例。

最佳答案

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/

相关文章:

javascript - 引用错误 JS 未为 Datamap 定义

javascript - 如何使用 jquery 将段落的原始文本格式放入 html 段落元素中?

javascript - 使用另一个表单元素的 textarea 值动态更新 h1 元素

javascript - screen.availHeight 和 window.height() 的区别

javascript - onclick 事件不适用于位置 :fixed

javascript - 单击时更改按钮名称并保存

javascript - JS 中从 XMLDocument 获取默认命名空间

java - 奇怪的 xml 解析行为

sorting - CouchDB - 按位置对文档进行排序

svn - 在 liferay 服务器启动时以编程方式将文件添加到文档和媒体库