(See also the versioning discussion.)
ES4 shall define normative values for the version parameter described in RFC 4329 as optional for the application/javascript and application/ecmascript Internet media types. From RFC 4329 section 3:
Updates of this document may introduce new optional parameters; implementations MUST consider the impact of such an update. For the application/ecmascript media type, implementations MUST NOT process content labeled with a “version” parameter as if no such parameter had been specified; this is typically achieved by treating the content as unsupported. This error handling behavior allows extending the definition of the media type for content that cannot be processed by implementations of [ECMA].
User agents implementing ES3.1 and ES4 MUST follow RFC 4329 by processing a version parameter for application/ecmascript, not ignoring it. For application/ecmascript, the version value 4 corresponds to ECMAScript 4. The version value 3.1 corresponds to ECMAScript 3.1. An example string containing the full type with version is “application/ecmascript;version=4”. Content with other explicit version values including 1, 2, 3, and 4.0 are not specified, and if sent to a peer implementing ES4 MAY be handled as unsupported content.
For the application/javascript type, the version values already in use are 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, and 1.7 (note that these are not specified by RFC 4329 or by any ECMA specification). Implementations of ES4 MUST process content of type application/javascript with version equal to 2 the same as they would application/ecmascript with version equal to 4.
The document’s default scripting language is used for all script that does not have an explicit language and version specified (e.g. event handlers, javascript: URLs, and script embedded in cascading stylesheets).
User agents are free to choose any default scripting language. It is expected that most current user agents will default to ES3.
The Content-Script-Type directive overrides the default script version in a document. It can be specified as an HTTP header or meta tag.
<meta http-equiv="Content-Script-Type" content="application/ecmascript;version=4">
The language and language version for a script tag is determined by the server-specified Content-Type header. If the server-specified Content-Type header does not specify a language version or the script is inline, a more specific type attribute on the script tag’s can be used to select the language and version. If neither the Content-Type header nor the type attribute specifies a version, and the script is interpreted using the document’s default scripting language version.
Examples:
<script>
// The document's default scripting language version is used
</script>
<script type="text/javascript">
// The document's default scripting language version is used
</script>
<script type="application/ecmascript;version=4">
// This script is interpreted as ES4.
</script>
<script src="code.js">
<!--
HTTP Content-Type is used to select the language and language version.
If the Content-Type does not specify a language, the document's default language is used.
If the Content-Type does matches the document's default language but does not specify a version, the document's default language version is used.
-->
</script>
<script type="application/ecmascript;version=4" src="code.js">
<!--
HTTP Content-Type is used to select the language and language version.
If the Content-Type does not specify a language, ES4 is used.
If the Content-Type specifies ECMAScript (or JavaScript) but no version, ES4 is used.
-->
</script>
ES4 implementations MUST define a new global ReadOnly and DontDelete property __MAX_ECMASCRIPT_VERSION__, whose value is a string reflecting the maximum ECMA-262 version supported by the user agent’s ES implementation.
Thus in order to operate with old and new user agents, an HTML file may contain:
<script type="text/javascript">
if (this.__MAX_ECMASCRIPT_VERSION__ >= 4)
document.writeln("<script type='application/ecmascript;version=4' src='es4-code.js'><\/script>");
else
document.writeln("<script type='text/javascript' src='es3-code.js'><\/script>");
</script>
This example is specific to a Web browser user agent, but similar examples can be given for other kinds of user agents.
The Language attribute of the script tag is deprecated.
The HTTP/1.1 specification allows user agents to send an Accept header containing compatible versions of ECMAScript in preference order. Servers may use this header to decide which language to reply with.
GET /code.js HTTP/1.1 Accept: application/ecmascript;version=4, application/ecmascript, */*
Changing global object bindings is incompatible with ES4.
If ES3 or ES3.1 code tries to overwrite these bindings before any ES4 code has executed in the current document, these bindings are changed, but further ES4 scripts will not execute in that document.
If ES3 or ES3.1 code tries to overwrite these bindings after any ES4 code has executed in the current document, the bindings will be treated as readonly and the attempt to change them will fail silently.