Script Positions
Scripts can load in three positions:Head
- Scripts that must run before page renders
- Critical libraries other scripts depend on
- Styles that prevent layout shift
Body Start
- Scripts that need to run early but after head
- Rarely needed—most scripts work fine at body end
Body End (Default)
- Most scripts (recommended default)
- DOM manipulation code
- Event handlers
- Animations and interactions
Setting Load Position
Script Priority
Within each position, scripts load in priority order. Lower numbers load first. Example:| Script | Position | Priority | Loads |
|---|---|---|---|
| GSAP Init | Head | 1 | 1st |
| Animations | Body End | 1 | 2nd |
| Scroll Effects | Body End | 2 | 3rd |
| Analytics | Body End | 10 | 4th |
Loading Attributes
For external scripts, you can also set loading attributes:Defer
- Downloads in background while HTML parses
- Executes after HTML is fully parsed
- Maintains execution order
- Best for: Most external scripts
Async
- Downloads in background while HTML parses
- Executes immediately when download completes
- Does NOT maintain order
- Best for: Independent scripts (analytics)
Neither
- Blocks HTML parsing until downloaded and executed
- Avoid unless necessary
Common Load Order Patterns
Animation Library + Custom Animations
Slider with Custom Config
Analytics (Non-blocking)
Critical CSS
Execution Timing (Wait for DOM)
Control when your JavaScript executes relative to the DOM being ready. This can be set per-script with three options:Auto-detect (Default)
GoodCraft Script analyzes your code and automatically determines if it needs the DOM to be ready. It detects patterns like:document.querySelector()andquerySelectorAll()document.getElementById()and similar selectorsdocument.bodyaccessclassListmanipulation- jQuery’s
$(document).ready()and$(function)
DOMContentLoaded. Otherwise, it executes immediately.
When auto-detect finds DOM patterns in your code, you’ll see a badge indicating “Will wait” along with the specific patterns detected.
Always Wait for DOM Ready
Force the script to always wait until the DOM is fully loaded:- Auto-detect isn’t catching your DOM usage
- You want guaranteed DOM availability
- Scripts that manipulate elements created by other scripts
Execute Immediately
Run the script as soon as it loads, without waiting: Use when:- Scripts that set up global variables or functions
- Code that doesn’t touch the DOM
- Performance-critical initialization
Setting Execution Timing
- Open your script in the editor
- Click the Scope button in the toolbar
- Scroll to Execution Timing
- Choose your preferred option
- Click Save Changes
Site-Wide DOM Ready Wrapping
In addition to per-script settings, you can enable a site-wide default in Site Settings → Wrap in DOM Ready. This applies to all scripts that haven’t specified their own timing preference.Debugging Load Order Issues
Script runs before element exists
Symptom:Cannot read property of null
Fix:
- Move script to Body End
- Enable DOM Ready wrapping
- Use component detection
Library not defined
Symptom:GSAP is not defined
Fix:
- Ensure library loads before your script
- Check library is in Head or has lower priority
- Verify library URL is correct
Scripts run out of order
Symptom: Code assumes something exists that doesn’t yet Fix:- Adjust priorities (lower = earlier)
- Use defer instead of async
- Check position (Head → Body Start → Body End)
Flash of unstyled content
Symptom: Page briefly shows without styles Fix:- Move critical CSS to Head
- Inline critical styles
- Use
<link rel="preload">for fonts
Best Practices
Default to Body End
Default to Body End
Unless you have a specific reason, load scripts at Body End. It’s the safest position for most code.
Use Defer for external scripts
Use Defer for external scripts
Defer gives you the best of both worlds: non-blocking download with predictable execution order.
Keep priorities simple
Keep priorities simple
Use 1, 2, 3 for ordered scripts. Use 10 for low-priority scripts like analytics. Don’t overthink it.
Test on slow connections
Test on slow connections
Use browser DevTools to simulate slow networks. This reveals timing issues that don’t appear on fast connections.
Document dependencies
Document dependencies
If Script B depends on Script A, note it in comments:
Site-Wide Settings
In Site Settings, you can configure defaults:- Bundle Scripts: Combine all scripts into one file
- Use Modules: Use ES6 module syntax (
type="module") - Wrap in DOM Ready: Auto-wrap all scripts