Part 1: Escaping Global Scope Chaos with <script type="module">
In Getting Ready for v2027 (I), we covered system requirements and environment configuration. Now, let's dive into the technical improvements in the generated code.
If you're upgrading from v2026 or earlier, one of the biggest changes in PHPMaker 2027 is how client-side scripts work. Previous versions split your custom JavaScript into two sections, Client Script and Startup Script, as a workaround for two limitations of classic <script> tags: DOM readiness and execution order.
In legacy pages, scripts were split like this:
<!DOCTYPE html>
<html>
<head>
<!-- 1. Client scripts in <head>: libraries & helper functions -->
<script src="./lib.js"></script>
<script src="./clientscript.js"></script>
</head>
<body>
<button id="save-btn">Save</button>
<!-- 2. Startup scripts before </body>: DOM access & initialization -->
<script src="./startupscript.js"></script>
</body>
</html>
You had to maintain this split for two reasons:
- The HTML wasn't parsed yet: If
startupscript.jsran inside<head>,document.getElementById('save-btn')returnednullbecause the<button>further down the page didn't exist yet. Placing startup scripts right before</body>guaranteed the DOM was ready. - Manual load order: Classic scripts execute immediately in the exact order they appear. Placing helper scripts at the top ensured their global functions existed before
startupscript.jscalled them at the bottom.
In v2027, Module Script (<script type="module">) solves both problems natively, letting you keep your scripts cleanly in <head> or load a single entry script.
How Module Scripts Eliminate the Split
1. Automatic defer makes the DOM ready even in <head>
Every <script type="module"> is deferred by default. When the browser encounters a module script in <head>, it:
- Downloads the file in the background without blocking HTML rendering.
- Waits until the entire HTML document has finished parsing before executing the code.
Because the DOM is guaranteed to exist when the module runs, you no longer need to push startup scripts to the bottom of <body> (or wrap initialization code in ew.ready / $(function () { ... }) just to wait for elements).
2. import handles dependency order automatically
Instead of relying on <script> tag order—and dumping every helper function onto the global window object—your module explicitly imports what it needs:
modulescript.js
import { formatDate } from './lib.js';
import { saveData } from './clientscript.js';
// Safe to access the DOM even though this script is loaded in <head>
const btn = document.getElementById('save-btn');
btn.addEventListener('click', () => {
saveData(formatDate(new Date()));
});
*.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Module Startup</title>
<!-- One clean entry point in <head> -->
<script type="module" src="./modulescript.js"></script>
</head>
<body>
<button id="save-btn">Save</button>
</body>
</html>
When the browser loads modulescript.js, it reads the import statements, fetches lib.js and clientscript.js in parallel, executes those dependencies first, and only runs modulescript.js once both its imports and the page's HTML are ready.
Key Differences from Classic <script> Tags
Switching to <script type="module"> also changes scoping and execution rules:
- Private module scope: Variables and functions declared at the top level of a module stay scoped to that file instead of polluting
window. - Strict mode by default:
"use strict"is always active inside modules. Silent bugs (such as accidentally creating a global variable by forgettingletorconst) throw clear errors immediately. - Executed only once: Even if you reference the same module in multiple
<script type="module">tags or import it across several files, the browser fetches and runs it only once and shares the exported bindings. - Inline modules work the same way: A
<script type="module"> ... </script>block without asrcattribute still gets its own private scope, strict mode, and automaticdefer.
Further Reading
- MDN: Modules on the web: Explains how
<script type="module">behaves in HTML and highlights every difference from classic scripts. - MDN: JavaScript modules: Complete overview of native browser
importandexportsyntax. - JavaScript.info: Modules, Introduction: Beginner-friendly walkthrough with interactive examples covering module scope,
defer, and strict mode.
(Next up: Part 2: Connecting Files with export and import)