What is MIME type "text/javascript"?

A MIME type is a string that tells browsers and other tools how to handle a particular kind of file.

text/javascript is a MIME type used to mark files containing JavaScript code in plain text. It tells browsers and other software that the content should be treated as executable JavaScript, enabling dynamic behavior on web pages.

When you include a script in an HTML page, the browser uses this MIME type to know it must execute the code. This is common with inline scripts or external files loaded with the <script> tag.

Files carrying JavaScript code may have many extensions. For example, files like JS, JSX, MJS, or even those used for JSON with comments like JSONC are tied to this MIME type. These variations support different development approaches and coding environments.

For more detailed technical information about MIME types, you can refer to resources such as MIME Types on MDN or visit the official IANA Media Types page.

Associated file extensions

Usage Examples

HTTP Header

When serving content with this MIME type, set the Content-Type header:


    Content-Type: text/javascript    
  

HTML

In HTML, you can specify the MIME type in various elements:


    <a href="file.dat" type="text/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', 'text/javascript');    
      res.end('Content here');    
    }).listen(3000);    
  

Associated file extensions

FAQs

Is text/javascript or application/javascript the correct MIME type?

text/javascript is currently the recommended standard for web development. While application/javascript was once the official IANA standard, it has been deprecated, and modern HTML specifications explicitly prefer text/javascript for maximum browser compatibility.

How do I fix "Refused to execute script" errors in the browser?

This error occurs when a server sends a JavaScript file with an incorrect header (like text/plain or text/html) while the X-Content-Type-Options: nosniff security header is active. To fix this, ensure your web server is configured to serve .js files with the Content-Type text/javascript.

How do I configure Apache to serve JavaScript files correctly?

You can ensure the correct MIME type by adding an AddType directive to your .htaccess or main configuration file. Add the line: AddType text/javascript .js .mjs to map the extensions to the correct type.

Should .mjs and .cjs files use text/javascript?

Yes, browsers generally expect all JavaScript code to be served as text/javascript, regardless of whether it is an ES Module (.mjs) or CommonJS (.cjs). While Node.js uses these extensions to distinguish module systems internally, web servers must send the standard MIME header for browsers to execute them.

Do I need to specify type="text/javascript" in HTML script tags?

No, in modern HTML5, the type attribute is optional for standard JavaScript. If you omit it (e.g., <script src="app.js"></script>), the browser defaults to treating the content as text/javascript. You generally only need the type attribute when using <script type="module">.

Why are JSONC or config files associated with text/javascript?

While standard JSON data uses application/json, files like JSONC (JSON with comments) or editor configurations (e.g., .sublime-settings) use JavaScript-style syntax that isn't valid strict JSON. Tools often identify these as text/javascript to enable correct syntax highlighting and comment parsing.

What are the security risks of allowing text/javascript uploads?

Files served with this MIME type are executable by the browser. If a web application allows users to upload files and subsequently serves them as text/javascript, it creates a high risk of Cross-Site Scripting (XSS) attacks. Always validate user uploads and serve untrusted content as text/plain or application/octet-stream.

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.