-1

Why google ignores type attribute in its script. Should I add the type="text/javascript" myself? What is the programmatic reason behind this?

Sample analytics code:

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'sample code', 'auto');
  ga('send', 'pageview');

</script>
1
  • Probably, HTML5. Of course, it isn't mandatory. The script will run anyway no matter the HTML version. Commented Sep 6, 2017 at 8:19

1 Answer 1

1

The reason is because all the browsers out there will default to 'text/javascript' if you do not specify a type. It's wasted characters and you shouldn't use it anymore. From MDN HTML Element Script:

Omitted or a JavaScript MIME type: For HTML5-compliant browsers this indicates the script is JavaScript. HTML5 spec urges authors to omit the attribute rather than provided a redundant MIME type. In earlier browsers, this identified the scripting language of the embedded or imported (via the src attribute) code. JavaScript MIME types are listed in the specification.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.