Skip to main content
HTML Validation

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

About This HTML Issue

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

<!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

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

Find issues like this automatically

Rocket Validator scans thousands of pages in seconds, detecting HTML issues across your entire site.

Help us improve our guides

Was this guide helpful?
🌍 Trusted by teams worldwide

Validate at scale.
Ship accessible websites, faster.

Automated HTML & accessibility validation for large sites. Check thousands of pages against WCAG guidelines and W3C standards in minutes, not days.

Scheduled Reports
API Access
Open Source Standards
€5 / week

1K Weekly

1,000 monthly checks. Cancel anytime.

Get 1K Weekly →

Join teams across 40+ countries