Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

What is the correct media type for JavaScript source code?

DZone's Guide to

What is the correct media type for JavaScript source code?

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Question: What media type should you use for JavaScript source code? Answer [via Brendan Eich] and explanations after the break.

The correct JavaScript media type

There are many JavaScript media types in use. Examples:
    application/ecmascript
    application/javascript
    application/x-ecmascript
    application/x-javascript
    text/ecmascript
    text/javascript
    text/javascript1.0
    text/javascript1.1
    text/javascript1.2
    text/javascript1.3
    text/javascript1.4
    text/javascript1.5
    text/jscript
    text/livescript
    text/x-ecmascript
    text/x-javascript
Which one of those is correct? RFC 4329 has the answer:
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”. Use of experimental and unregistered media types, as listed in part above, is discouraged. The media types,
  • application/javascript
  • application/ecmascript
which are also defined in this document, are intended for common use and should be used instead.
However: While text/javascript is considered obsolete, it might still be necessary to use it if you want your code to run on Internet Explorer 8. Quoting machineghost on Stack Overflow [via Gezim Hoxha]:
Moral of the story: if you want IE8 to work DO NOT use "application/javascript" for your JS files' MIME type [a.k.a. media type].

Uses of the media type

The (internet) media type of JavaScript source code is mainly needed in two cases:
  • When serving code via a web server.
  • As the value of an optional attribute of the script tag:
        <script type="application/javascript">
            ...
        </script>
    
    This scheme might be extended in the future to switch on support for ECMAScript.next (which will probably eventually be called ECMAScript 6):
        <script type="application/javascript;version=6">
            ...
        </script>
    

 

From http://www.2ality.com/2011/08/javascript-media-type.html

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}