Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JavaScript's document.open support the MIME type 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.org/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.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

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

like image 508
Monkeybrain Avatar asked Aug 30 '16 17:08

Monkeybrain


1 Answers

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();
});
like image 82
Jeremy Avatar answered Sep 28 '22 05:09

Jeremy