Is sending plain text HTML as just the plain text bad?

Viewed 138

If I put:

Welcome

Instead of:

<!DOCKTYPE html>
<html>
  <body>
    Welcome
  </body>
</html>

In my index.html, will that cause problems on some browsers?

2 Answers

Browsers rely on the HTTP Content-Type header to determine how content should be parsed.

"Sending as plain text" usually refers to using Content-Type: text/plain, but I assume your index.html is always sent as text/html, and the question is about the contents of the file only.

Using Content-Type: text/html invokes the HTML parser, which produces almost the same DOM for both inputs, except for the DOCTYPE declaration (assuming you meant <!DOCTYPE html>, not DOCKTYPE).

In general, leaving out the DOCTYPE puts the page in the so-called "quirks mode", which has a number of effects in certain edge-cases.

When a browser tries to construct the DOM tree it will find itself with the stack of open elements empty. Thus the insertion mode will be set to initial, as stated in the reference link:

The insertion mode is a state variable that controls the primary operation of the tree construction stage. Initially, the insertion mode is "initial".

So, you get to Initial insertion mode and follow a series of Anything else cases that, as they go through several insertion modes, automatically proceed to create and close <html>, <head> and <body> tags and, within the latter, append your plain text.

However know that if you send just plain text with, for example, a .txt file the browser will treat also any eventual tag as plain text and wrap everything into a <pre> tag, since the content-type will be likely set to text/plain and the browser assumes you want to see everything as plain text.

Instead in your case putting plain text in an .html file will still set the content-type to text/html making the browser recognize eventual tags written there, without wrapping your code into a <pre> tag.
So in the last case there will be no difference in the DOM tree created by the two contents shown in your question.

Except actually for one: in your case the DOCTYPE declaration is omitted and this will make the browser render your page using a Quirk mode instead of a standard one, which may lead to unwanted results.

Related