October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Should You Put JavaScript Before Instead of in the ?

For DOM-dependent external JavaScript, defer in the head is often a sensible default. A body-end script is valid too, but is not a guaranteed speed or SEO fix.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Not as a blanket rule. For an external classic JavaScript file that needs the page’s DOM, a strong default is to keep it in the <head> and add defer. The browser can then parse the HTML while the file downloads, and run the script after parsing. Putting a script just before </body> is also valid, but its location alone does not guarantee faster loading or better SEO.

Why script placement matters

An ordinary external classic script in the head, without async or defer, can pause HTML parsing while the browser fetches and executes it. That means the browser may not continue building the page until the script is ready.

With defer, an external classic script downloads while parsing continues, then runs after the document has been parsed. Deferred classic scripts run in the order they appear in the HTML, which makes this option suitable for application code that depends on another script loaded earlier. MDN’s script-element reference documents this behavior.

Choose the loading behavior your code needs

Script type or placement What happens When it fits
Classic external script in the head, no attribute May block HTML parsing while it downloads and executes. Use only when that blocking behavior is intentional.
Classic external script with defer Downloads without pausing parsing; executes after parsing, in document order relative to other deferred classic scripts. Often a good choice for DOM-dependent code or ordered application scripts.
Classic external script with async Executes as soon as it is available; order relative to other scripts is not guaranteed. For scripts that are independent and do not rely on execution order.
Classic script immediately before </body> Markup before the script has been parsed when the script runs, unless other behavior changes the timing. A valid placement choice when the code should run after preceding markup is parsed.
Inline classic script with defer defer has no effect without a src attribute. Do not rely on defer to postpone inline code.
Module script Modules are deferred by default; adding async changes their execution timing. Use module loading semantics when the code is authored as a JavaScript module.

See MDN’s script-element reference for the attributes and their behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When should you use async?

Use async only when the script can run independently of other scripts and page code. Because it runs as soon as it becomes available, it may execute before or after another script regardless of their order in the document. It is therefore not a substitute for defer when one script depends on another. MDN’s async-script documentation explains the timing distinction.

What about a JavaScript image slider in the header?

The slider’s position on the page does not, by itself, require its JavaScript to be placed in the head. Decide based on what the code needs:

  • If the slider script must find elements in the parsed HTML, an external script with defer is a straightforward choice.
  • If it depends on a library or another script, preserve the required order; deferred classic scripts keep their document order, while async scripts do not.
  • If the slider is part of the visible content users need immediately, assess its real loading and rendering behavior on the page rather than assuming that moving the JavaScript changes when its images appear.

A header slider may warrant performance attention because it is visible early, but that alone does not establish which placement is fastest for a particular implementation. Measure the page and examine the actual bottleneck.

Account for DOMContentLoaded

The DOMContentLoaded event waits for deferred and module scripts to download and execute. An async script or a script inserted dynamically may execute after that event has already fired. If code relies on the event, account for this timing rather than assuming every script runs beforehand. MDN’s DOMContentLoaded reference describes what the event waits for.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Does putting JavaScript before the footer improve SEO or speed?

Moving JavaScript to the end of the body is not, by itself, proof of a speed improvement or an SEO advantage. It can ensure that preceding markup has been parsed before the script executes, but the result for a page depends on its code, dependencies, and loading behavior. The 2012 SitePoint discussion that prompted this question contains conflicting opinions, not controlled measurements establishing a present-day rule. Read the original SitePoint discussion.

For a page you maintain, compare its actual behavior with the script placed at the body end and with an appropriate loading attribute. Avoid treating either position as a universal optimization.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.