# The “charset” attribute on the “script” element is obsolete.

> Canonical HTML version: https://rocketvalidator.com/html-validation/the-charset-attribute-on-the-script-element-is-obsolete
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

The `charset` attribute was historically used to declare the character encoding of an external script file when it differed from the document's encoding. In modern HTML, this attribute is obsolete because the HTML specification now requires that the character encoding of an external script must match the encoding of the document itself. Since the document's encoding is already declared via `<meta charset="UTF-8">` (or through HTTP headers), specifying it again on individual `<script>` elements is redundant and no longer valid.

If your external script files use a different encoding than your HTML document, the correct solution is to convert those script files to match the document's encoding (typically UTF-8) rather than using the `charset` attribute. UTF-8 is the recommended encoding for all web content and is supported universally across browsers.

This matters for **standards compliance** — the HTML living standard explicitly marks this attribute as obsolete. It also affects **maintainability**, since having encoding declarations scattered across script tags can create confusion about which encoding is actually in effect. Browsers may also ignore the attribute entirely, leading to unexpected behavior if you're relying on it.

## How to fix it

1. **Remove the `charset` attribute** from all `<script>` elements.
2. **Ensure your document declares its encoding** using `<meta charset="UTF-8">` inside the `<head>`.
3. **Convert any script files** that aren't already UTF-8 to use UTF-8 encoding. Most modern code editors can do this via "Save with Encoding" or a similar option.
4. While you're at it, you can also remove `type="text/javascript"` — this is the default type for scripts and is no longer needed.

## Examples

### Incorrect: using the obsolete `charset` attribute

```html
<script src="app.js" type="text/javascript" charset="UTF-8"></script>
```

This triggers the validation warning because `charset` is obsolete. The `type="text/javascript"` is also unnecessary since it's the default.

### Incorrect: `charset` on an inline script

```html
<script charset="UTF-8">
  console.log("Hello");
</script>
```

The `charset` attribute was only ever meaningful for external scripts, and even in that context it is now obsolete.

### Correct: clean script element

```html
<script src="app.js"></script>
```

### Correct: ensuring encoding is declared at the document level

```html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>My Page</title>
    <script src="app.js"></script>
  </head>
  <body>
    <p>Content here.</p>
  </body>
</html>
```

The `<meta charset="UTF-8">` declaration in the `<head>` covers the encoding for the entire document, including all linked scripts. No additional `charset` attributes are needed on individual elements.
