31

I was reading a book about learning JavaScript, and there was these paragraphs:

...in middle of 1997, Microsoft and Netscape, with associate of European Computer Manufactures Association,

released the first version of a standard that named ECMAScript or with official form ECMA-262...

As much as I was found in this book and something like this, JavaScript and ECMAScript are the same and are different just in name.

From other hand, in Dreamweaver, bracket, and some other editors, there's some autocomplete suggestion like this:

enter image description here

when I want to add a script tag to my page.

I want to know if there are differences between ECMAScript and Javascript and when should I use text/javascript or text/ecmascript?

Ramesh
  • 13,043
  • 3
  • 52
  • 88
Kiyarash
  • 2,437
  • 7
  • 32
  • 61
  • 12
    you don't need either anymore... – dandavis Apr 29 '14 at 17:03
  • 2
    `Javascript` is what everyone else calls it, `EMCAscript` is the official ISO-backed specification, because the JS standard is under the stewardship of ECMA. – Marc B Apr 29 '14 at 17:03
  • 7
    Voting to reopen, as the questions linked as duplicates of this one don't specifically address when to use the `text/ecmascript` MIME type. – legoscia Apr 29 '14 at 17:41
  • 3
    I checked links in top of the question and as the @legosica said; there is no direct reference to the fact... how can I vote for reopen? –  Apr 30 '14 at 02:28
  • I think you can't because of your reputation... – Kiyarash Apr 30 '14 at 02:30
  • Found some details here which could be useful for someone.. http://stackoverflow.com/a/4271687/2860358 – Sachin Sep 22 '15 at 18:31
  • possible duplicate of [What is the difference between javascript and ecma script?](http://stackoverflow.com/questions/4271676/what-is-the-difference-between-javascript-and-ecma-script) – Sachin Sep 22 '15 at 18:32
  • 2
    It would be much better if it was called ECMAscript to avoid confusion with Java – Hacktisch Sep 23 '15 at 20:36

2 Answers2

24

TL;DR Just omit the type tag, if you don't care about old browsers.

ECMAScript is a language specification standardized by ECMA International as ECMA-262 and ISO/IEC 16262. JavaScript is a programming language that implements this specification. ECMAScript exists in several editions. The latest edition is the 6th (in 2016), but most JavaScript implementations are only 5th edition compliant so far.

Besides ECMAScript common JavaScript implementations usually add more functionality, which might be standardized by other institutions (like the W3C) or might be proprietary (aka "browser-specific") features of the specific implementation. So you could say, that ECMAScript represents a subset of JavaScript.

However, the MIME types for JavaScript code are defined in the RFC 4329 document, which states that text/javascript and text/ecmascript are both obsolete and should be replaced by application/javascript and application/ecmascript:

Use of the "text" top-level type for this kind of content is known to be problematic. This document thus defines text/javascript and text/ ecmascript but marks them as "obsolete".

The RFC defines stricter processing rules for the application/ecmascript than for application/javascript, but this refers to the handling of MIME-type parameters and the character encoding and not to the interpretation of the code itself:

In the cited case, implementations of the types text/javascript, text/ecmascript, and application/javascript SHOULD and implementations of the type application/ecmascript MUST implement the requirements defined in this section: [...]

For the application/ecmascript media type, implementations MUST NOT process content labeled with a "version" parameter as if no such parameter had been specified; [...]

The following error processing behavior is RECOMMENDED for the media types text/javascript, text/ecmascript, and application/javascript, and REQUIRED for the media type application/ecmascript.

Apart from the RFCs, there is also the HTML5-Standard by the W3C: Older versions of it say, that the default value for an empty type attribute is application/javascript, but newer versions don't mention any specific MIME-type anymore. Instead they define any script tag with no type or a MIME-type as a so-called "classic script":

Omitting the attribute, setting it to the empty string, or setting it to a JavaScript MIME type essence match, means that the script is a classic script, to be interpreted according to the JavaScript Script top-level production. Classic scripts are affected by the async and defer attributes, but only when the src attribute is set. Authors should omit the type attribute instead of redundantly setting it.

In general I would omit the type attribute (as recommended by the HTML5-Standard) or use type="text/javascript", if you have to support older browsers. A HTTP-Server should serve JavaScript code as application/javascript.

Pang
  • 9,564
  • 146
  • 81
  • 122
Torben
  • 6,317
  • 1
  • 33
  • 32
  • I've never seen this `application/javascript` or `application/ecmascript` in the intellisense while providing `type` attribute of `script` tag. I work on asp.net and asp.net mvc applications using Visual Studio. – RBT Aug 01 '17 at 11:05
  • @RBT Even though your code completion does not suggest these types, they are valid. However, with HTML5 you don't need the type-attribute at all. – Torben Aug 02 '17 at 20:29
  • 1
    ohh. Thanks for the info. I saw it [here](https://stackoverflow.com/a/4195504/465053) that `type` attribute has a default value `text/javascript` starting HTML5 so it has become optional. Nice. Less typing :). I hope some time down the line the default will become `application/javascript` in next version of HTML as per the RFC you've cited. – RBT Aug 03 '17 at 01:01
  • For web-applications, we should most likely stick to `text/javascript`, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textjavascript – Null May 02 '19 at 12:49
  • [@Malvoz](/users/9314312/malvoz) Thanks for the hint, I've updated my answer. – Torben Jul 31 '19 at 00:20
  • when i first became aware of the diff javascript mime types, i thought the `application` one would have been reserved for and indicative of electron applications and the like. guess i was wrong – oldboy Nov 25 '20 at 01:54
1

The current version of the HTML standard (last updated 16 July 2019) says that the language type specified with the type attribute of the <script> tag falls back to text/javascript if ommitted.

Also that specification specifies that:

Servers should use text/javascript for JavaScript resources. Servers should not use other JavaScript MIME types for JavaScript resources, and must not use non-JavaScript MIME types.

Sebastian Barth
  • 4,079
  • 7
  • 40
  • 59