Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using type="text/plain" for JavaScript?

I was looking at setting this up, simply out of curiosity, however I was a little bemused when they stated for this to work you need to:

Find any Javascript elements that set Analytics cookies. Examples might include Google Analytics and StatCounter. Modify the script tag so that the type attribute is "text/plain" rather than "text/javascript"

Would this cause any problems with certain web browsers? Would it cause the HTML to no longer validate?

Also, does the "type" attribute even really serve a purpose anymore? I've only ever seen it assigned "text/JavaScript" before?

like image 475
Sean Avatar asked Aug 23 '12 11:08

Sean


People also ask

Should I use type text JavaScript?

No, it is not necessary if you use HTML5. "Differences Between HTML 4.01 and HTML5 In HTML5, the type attribute is no longer required for JavaScript. The default value is "application/javascript"." Source: https://www.w3schools.com/tags/att_script_type.asp To use HTML5 write <!

What is plain text in JavaScript?

A type indicating Javascript runs it as Javascript. A type of "module" runs it as a Javascript module. any other type causes it not to run, unless the browser has a proprietary interpreter installed for that very type. text/plain is fairly safely guaranteed never to execute something.

What does type text/JavaScript do?

It allows browsers to determine if they can handle the scripting/style language before making a request for the script or stylesheet (or, in the case of embedded script/style, identify which language is being used).


2 Answers

It does not cause problems, if the intent is that browsers do not interpret the content of the element as script code but just as text data that is not rendered. It’s there for scripts to use it, but otherwise it’s ignored. Well, in some browsers, the content might be made visible using CSS, but by default it’s not shown.

Using <script type="text/plain"> is valid by HTML specs. Even <script type="Hello world ☺"> is valid, though it violates the prose requirement that the attribute value be a MIME type. The specs do not specify its meaning, but the only feasible interpretation, and what browsers do in practice, is that it is not in any scripting language and no execution as script is attempted.

So type="text/plain" may be used to intentionally prevent execution of a script, while still keeping it in the source. It may also be used to carry bulks of character data used for some processing.

The type attribute may serve purposes like this, and it can also be used to specify scripting languages other than JavaScript (rarely used, but still possible in some environments). Using the type attribute just to specify JavaScript is not needed, and cannot be recommended: the only thing that you might achieve is errors: if you mistype, e.g. type="text/javascirpt", the content will be regarded as being in an unknown language, hence ignored.

like image 123
Jukka K. Korpela Avatar answered Oct 13 '22 03:10

Jukka K. Korpela


Would this cause any problems with certain web browsers?

No

Would it cause the HTML to no longer validate?

No

Also, does the "type" attribute even really server a purpose anymore?

Browsers use it to decide what interpretor to run code through (or if they should download externally srced code at all).

Setting it to text/plain sets it to a type that browsers won't have interpretors for (since it isn't a programming language), which is the point.

like image 1
Quentin Avatar answered Oct 13 '22 02:10

Quentin