admin管理员组

文章数量:1287833

Some documentation suggests that document.open() supports taking a MIME type as its first parameter. For example: HTML DOM Open Method (Dottoro).

I also have an ancient JavaScript textbook which claims you can pass MIME types to document.open(). But most docs I look at say otherwise:

Was this a parameter supported in early JavaScript which has since been removed?

I don't see it in the DOM specifications:

  • .html#ID-1006298752
  • .html#ID-72161170

This is just for my interest; I don't have a specific use case for the parameter.

Some documentation suggests that document.open() supports taking a MIME type as its first parameter. For example: HTML DOM Open Method (Dottoro).

I also have an ancient JavaScript textbook which claims you can pass MIME types to document.open(). But most docs I look at say otherwise:

  • https://developer.mozilla/en-US/docs/Web/API/Document/open

Was this a parameter supported in early JavaScript which has since been removed?

I don't see it in the DOM specifications:

  • https://www.w3/TR/REC-DOM-Level-1/level-one-html.html#ID-1006298752
  • https://www.w3/TR/DOM-Level-2-HTML/html.html#ID-72161170

This is just for my interest; I don't have a specific use case for the parameter.

Share Improve this question edited Mar 6, 2018 at 18:22 Jeremy Banks 130k88 gold badges358 silver badges381 bronze badges asked Aug 30, 2016 at 17:09 MonkeybrainMonkeybrain 8608 silver badges25 bronze badges 2
  • I was trying this today (replacing the active document with an application/json or text/plain), and it didn't seem to work in Chrome, but I wasn't sure if I was doing something wrong. Could someone poke around the browsers (probably the source code) and see whether this capability is still available in some way? Or what the restrictions are on the applicable MIME types -- perhaps it's only meant for distinguishing HTML and XHTML, and other types are ignored. Looking for any info like this, in modern-day browsers, for the bounty. – Jeremy Banks Commented Mar 4, 2018 at 2:51
  • If support is inconsistent, is there a way to feature-detect it? – Jeremy Banks Commented Mar 4, 2018 at 3:02
Add a ment  | 

3 Answers 3

Reset to default 6

Chrome

Chrome does not use the type parameter.

A V8Document.openMethod() method checks the airity of arguments to document.open(...) then invokes either v8Document.open1Method() or v8Document.open2Method(). v8Document.open2Method() doesn't even read the first (type) argument that it's provided. v8Document.open1Method() does read it, and set it to a default value of "text/html" if it's undefined. It then passes the type value to a Document.open() method, but from there it's ignored.

Firefox

Firefox uses the type parameter, but the only accepted non-default value is "text/plain".

A nsHTMLDocument::Open() method sets type to "text/html" if the argument is missing, then invokes another overload. The overload converts all type values other than "text/html" to "text/plain", and then applies that content-type to the document.

Detection

The .contentType property can tell us the type of a document we have. We can't use this to feature-detect in advance, but we can use it to check what type the document was actually opened as, and modify our output accordingly. For example:

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();
});

You can specify mime type in a encoded URL that you place as a link.

buffer = ...
var blob = new Blob([buffer], {type: 'text/plain'})
var dataUri = window.URL.createObjectURL(blob)
window.open(dataUri)

Some context: document.write and document.open have some issues, and for that reason their use is discouraged. See

https://developers.google./web/updates/2016/08/removing-document-write https://www.sitepoint./insert-in-place-without-documentwrite/

It is an old DOM API, and back in the day browser maker were not coordinated like today on implementing the w3 spec. (which was the reference at this time, nowadays it is more whatwg)

So there are maybe some browser that have a MIME parameter indeed for document.open, but relying on it could cause error sometimes. So it is not reliable. Use alternatives when possible. In fact it might be removed pletely some day. It does not exist in the last dom whatwg spec https://dom.spec.whatwg/

The answer is: It depends on the browser. From the dottoro link provided it seems that Safari and Chrome never supported it.

To be technically correct it is not a JavaScript API but a DOM one.

本文标签: domDoes JavaScript39s documentopen support the MIME type parameterStack Overflow