12

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?

3 Answers 3

19

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I may be missing something really obvious here (and if I am I apologise) but you've said that "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" so surely if you did this to JavaScript code it would no longer execute?
e.g. if applied to the google analytics code, Google analytics would no longer work on your site
@SeanDunwoody — Yes, that's the point. You don't want the JS executing without consent for tracking.
I thought the point was to get cookie consent, not disable all JavaScript on the page even if cookie consent is granted . . . I could do that without a plugin. Unless the plugin sets the type attribute once consent is granted (it probably does, and I've skipped past the bit where it says that)
@SeanDunwoody — The instructions you quoted said "Find any Javascript elements that set Analytics cookies", not all JS. If you don't disable them, then the cookies will be set when the page is loaded … before consent is given. Running the JavaScript after consent is given is almost certainly what the plugin does.
|
1

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.

1 Comment

The behaviour of the type attribute is well documented on MDN. In short, no type defaults to running it as 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
1

Giving JS code the text/plain type is perfectly ok and will effectively disable it. To enable the script after the page was loaded you will need to have JS code to rewrite the type to text/javascript on the fly. Cookie blocking is a common example. Insert a cookie-setting script with text/plain and change to text/javascript when the user gives cookie consent. This will execute the script immediately, on the current page, no need to reload it.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
I have been testing this today, and I don't think it's true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.