HTML <script> type Property: Defining the Script Type

The type attribute of the HTML <script> tag specifies the MIME type of the script being included or executed. It tells the browser how to interpret the content of the script. While traditionally used for JavaScript (text/javascript), its role has evolved with the introduction of modern JavaScript standards and other scripting languages.

Purpose of the type Attribute

The main purposes of the <script> type attribute are to:

  • Define the script’s content type: Inform the browser about the type of script being used (e.g., JavaScript, module, etc.).
  • Enable support for different scripting languages: Allow the use of languages other than standard JavaScript.
  • Support modern JavaScript modules: Indicate that the script contains ES modules.

Syntax

<script type="mime_type">
  // Your script code here
</script>

<script src="script.js" type="mime_type"></script>

Values for the type Attribute

Here’s a table explaining the most common values for the type attribute:

Value Description
`text/javascript` (Deprecated) Indicates standard JavaScript code. While technically the default, it is best practice to omit it for modern HTML5.
`module` Indicates that the script is an ECMAScript module. Allows the use of `import` and `export` statements.
`importmap` Specifies an import map for the module. This is a JSON object that maps module names to URLs.
`text/jsx` Indicates JSX syntax (JavaScript XML), commonly used in React applications.
`text/typescript` Indicates TypeScript code, which needs to be transpiled to JavaScript before execution.
*Other MIME types* Specifies other scripting languages or formats. The browser must support the specified type.

Note: For modern JavaScript, the type attribute can often be omitted as browsers default to text/javascript. However, for modules and other script types, specifying the type is essential. πŸ’‘

Examples

Let’s explore different scenarios using the <script> type attribute, starting from basic JavaScript inclusion to modern module usage.

Example 1: Including Standard JavaScript

In modern HTML5, including standard JavaScript often doesn’t require specifying the type attribute.

<!DOCTYPE html>
<html>
  <head>
    <title>Standard JavaScript Example</title>
  </head>
  <body>
    <p id="standardJsOutput">This is a standard JavaScript example.</p>

    <script>
      // Standard JavaScript code
      document.getElementById("standardJsOutput").textContent =
        "Hello from JavaScript!";
    </script>
  </body>
</html>

In this example, the script updates the text of a paragraph element.

Example 2: Using ES Modules

To use ECMAScript modules, set the type attribute to "module". This enables the use of import and export statements.

<!DOCTYPE html>
<html>
  <head>
    <title>ES Module Example</title>
  </head>
  <body>
    <p id="esModuleOutput">This is an ES module example.</p>

    <script type="module">
      // ES Module code
      import { updateText } from "./module.js";
      updateText("esModuleOutput", "Hello from ES module!");
    </script>

    <script>
      /*
      * Create a file named `module.js` in the same directory as the HTML file with the following content
      */
      // export function updateText(elementId, text) {
      //   document.getElementById(elementId).textContent = text;
      // }
    </script>
  </body>
</html>

Here, the script imports a function from a module and uses it to update the text. Make sure you create a file named module.js in the same directory as the HTML file with the content provided.

Example 3: Using JSX

To include JSX (JavaScript XML) syntax, specify the type as "text/jsx". Note that you typically need a build process (e.g., Babel) to transpile JSX into standard JavaScript.

<!DOCTYPE html>
<html>
  <head>
    <title>JSX Example</title>
  </head>
  <body>
    <div id="jsxOutput"></div>

    <script type="text/jsx">
      // JSX code (requires transpilation)
      ReactDOM.render(
        <h1>Hello, JSX!</h1>,
        document.getElementById('jsxOutput')
      );
    </script>

    <script
      src="https://unpkg.com/react@17/umd/react.development.js"
      crossorigin
    ></script>
    <script
      src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"
      crossorigin
    ></script>
  </body>
</html>

This example renders a “Hello, JSX!” heading using React. Remember that JSX needs to be transpiled into JavaScript for the browser to understand it. We have included React CDN here.

Example 4: Using TypeScript

For TypeScript code, set the type attribute to "text/typescript". TypeScript code needs to be transpiled into JavaScript before it can be executed in the browser.

<!DOCTYPE html>
<html>
  <head>
    <title>TypeScript Example</title>
  </head>
  <body>
    <p id="typescriptOutput">This is a TypeScript example.</p>

    <script type="text/typescript">
      // TypeScript code (requires transpilation)
      let message: string = "Hello from TypeScript!";
      document.getElementById("typescriptOutput").textContent = message;
    </script>
  </body>
</html>

In this example, TypeScript code is used to update the text of a paragraph element. This code would need to be transpiled into JavaScript using the TypeScript compiler.

Example 5: Using importmap

The importmap type is used to define how module specifiers are resolved, allowing you to map module names to specific URLs.

<!DOCTYPE html>
<html>
  <head>
    <title>Importmap Example</title>
  </head>
  <body>
    <p id="importmapOutput">This is an importmap example.</p>

    <script type="importmap">
      {
        "imports": {
          "my-module": "/modules/my-module.js"
        }
      }
    </script>

    <script type="module">
      import { doSomething } from 'my-module';
      doSomething("importmapOutput", "Hello from my-module!");
    </script>

    <script>
      /*
      * Create a folder named `modules` and a file named `my-module.js` inside it, with the following content
      */
      // export function doSomething(elementId, text) {
      //   document.getElementById(elementId).textContent = text;
      // }
    </script>
  </body>
</html>

Here, the importmap maps the module specifier "my-module" to the URL "/modules/my-module.js". Make sure you create a folder named modules and a file named my-module.js inside it, with the content provided.

Best Practices

  • Omit type="text/javascript": For standard JavaScript, it’s best to omit the type attribute.
  • Specify type="module" for ES Modules: Always include type="module" when using ES modules.
  • Use Appropriate MIME Types: Ensure you use the correct MIME type for non-JavaScript languages.
  • Transpile When Necessary: Remember to transpile JSX and TypeScript code into JavaScript before running it in the browser.

Browser Support

The <script> tag and its type attribute are widely supported across all modern browsers. However, support for specific MIME types (e.g., text/jsx, text/typescript) depends on whether the browser can handle them directly or requires a transpilation step.

Conclusion

The type attribute of the HTML <script> tag is essential for defining the content type of the script. While it’s often optional for standard JavaScript, it’s crucial for modern modules, JSX, TypeScript, and other scripting languages. Understanding and using the type attribute correctly ensures that your scripts are properly interpreted and executed by the browser, enabling you to leverage the full power of modern web development. πŸš€