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:
This is just for my interest; I don't have a specific use case for the parameter.
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 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.
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();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With