What is MIME type "application/javascript"?
A MIME type is a string that tells browsers and other tools how to handle a particular kind of file.
application/javascript signals that a file holds JavaScript code. It directs browsers and servers to treat the file as a script that adds interactive behavior to web pages.
The MIME type is vital for ensuring that code runs correctly and securely in modern web environments.
- Ensures browsers activate the JavaScript engine for dynamic behavior
- Supports client-side interactivity on websites
- Facilitates integration with server-side templating systems
- Standardizes how script files are treated across different platforms
Files that use this MIME type include those with extensions such as JS, ES, SNAP, and JS.ERB.
Associated file extensions
Usage Examples
HTTP Header
When serving content with this MIME type, set the Content-Type header:
Content-Type: application/javascript
HTML
In HTML, you can specify the MIME type in various elements:
<a href="file.dat" type="application/javascript">Download file</a>
Server-side (Node.js)
Setting the Content-Type header in Node.js:
const http = require('http');
http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/javascript');
res.end('Content here');
}).listen(3000);
Associated file extensions
FAQs
Should I use application/javascript or text/javascript?
While application/javascript was historically recommended as the standard by IANA (RFC 4329), text/javascript has been reinstated as the preferred type in modern specifications (RFC 9239). However, modern browsers and servers support both types interchangeably, so application/javascript remains a valid and functional choice for serving JS files.
How do I configure Apache to serve JS files as application/javascript?
You can enforce this MIME type by modifying your .htaccess file or main configuration. Add the line AddType application/javascript .js to ensure that files with the .js extension are served with the correct header instead of the default text type.
How do I set the correct MIME type for JavaScript in Nginx?
In Nginx, MIME types are usually defined in the mime.types file included in your configuration. To manually set it, ensure your http or server block contains: types { application/javascript js; }. This maps the extension to the MIME type.
Why is my browser downloading the .js file instead of running it?
This usually happens if the server is sending the file with a generic binary MIME type like application/octet-stream or application/force-download. To fix this, configure your web server to send the Content-Type: application/javascript header so the browser knows to execute the script rather than save it.
What is the security risk of incorrect JavaScript MIME types?
If a server allows users to upload files and serves them with an incorrect MIME type, browsers might try to "sniff" the content and execute malicious scripts (XSS). To prevent this, always serve scripts with the correct type and send the X-Content-Type-Options: nosniff header.
How are .js.erb files related to this MIME type?
Files with the .js.erb extension are Ruby on Rails templates that embed Ruby code within JavaScript. The server processes the Ruby code first, and the resulting output is sent to the browser as standard JavaScript, which should carry the application/javascript or text/javascript MIME type.
Does this MIME type work with ES Modules (.mjs)?
Yes, but ES Modules (often using .mjs or .js) are very strict about MIME types. If you load a module using <script type="module">, the server must return a valid JavaScript MIME type like application/javascript or the module will fail to load with a network error.
General FAQ
What is a MIME type?
A MIME (Multipurpose Internet Mail Extensions) type is a standard that indicates the nature and format of a document, file, or assortment of bytes. MIME types are defined and standardized in IETF's RFC 6838.
MIME types are important because they help browsers and servers understand how to process a file. When a browser receives a file from a server, it uses the MIME type to determine how to display or handle the content, whether it's an image to display, a PDF to open in a viewer, or a video to play.
MIME types consist of a type and a subtype, separated by a slash (e.g., text/html, image/jpeg, application/pdf). Some MIME types also include optional parameters.
How do I find the MIME type for a file?
You can check the file extension or use a file identification tool such as file --mime-type on the command line. Many programming languages also provide libraries to detect MIME types.
Why are multiple MIME types listed for one extension?
Different applications and historical conventions may use alternative MIME identifiers for the same kind of file. Showing them all helps ensure compatibility across systems.