# “script” element between “head” and “body”.

> Canonical HTML version: https://rocketvalidator.com/html-validation/script-element-between-head-and-body
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

A `<script>` element sits between the closing `</head>` tag and the opening `<body>` tag, where the HTML parser doesn't expect any elements.

After `</head>`, the parser expects the `<body>` start tag, with nothing but whitespace or comments before it. When it finds a `<script>` there, browsers move the element back into the `<head>` and run it from there, so the page usually works. The markup is still invalid. An analytics or tag manager snippet pasted right after `</head>` is a common cause, and so is a theme or plugin that prints its scripts there.

The validator treats this error as fatal. Right after it, the report shows "Cannot recover after last error. Any further errors will be ignored." and nothing later in the page gets checked. Other HTML problems below this point stay hidden until you fix it.

To fix it, move the `<script>` element inside the `<head>`, before `</head>`. Browsers already run it from there, so the page behaves the same after the change. A script can also go anywhere inside the `<body>`, for example right before `</body>`.

## Invalid example

```html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>My page</title>
  </head>
  <script src="analytics.js"></script>
  <body>
    <p>Hello, world!</p>
  </body>
</html>
```

## Valid example

```html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>My page</title>
    <script src="analytics.js"></script>
  </head>
  <body>
    <p>Hello, world!</p>
  </body>
</html>
```
