In JavaScript, the Document beforescriptexecute event once fired when a static<script> was about to start executing (MDN). It does not fire for scripts added dynamically (for example with appendChild()).
Register a listener with addEventListener. Check readiness and browser support before you rely on this event.
Syntax
Listen for beforescriptexecute. Start with this line.
JavaScript
addEventListener("beforescriptexecute", fn)
Call
What it does
addEventListener("beforescriptexecute", fn)
Primary signature.
Built into the browser. Listen with document.addEventListener("beforescriptexecute", …) — no import required.
Library declarations
JavaScript
addEventListener("beforescriptexecute", fn)
Parameters
Parameter
Description
Static <script>
Required. Dynamic scripts were skipped (MDN)
Modern path
Required. CSP / modules / no untrusted inject
Return Value
Return Value
Description
*
Fires as a Document event (listener receives the Event object).
JavaScript Examples
Example 1 — addEventListener("beforescriptexecute")
MDN style: listen on document and read the about-to-run script’s id (legacy Firefox only).
JavaScript
function starting(e) {
console.log(`Starting script with ID: ${e.target.id}`);
}
document.addEventListener("beforescriptexecute", starting);
console.log("Listener attached (may never fire today)");
Define a handler.starting logs event.target.id when the event fires.
Register on document. Same pattern as any DOM event via addEventListener.
Expect silence today. Most browsers never dispatch this event.
When it did fire.event.target was the static <script> about to run.
Example 2 — Cancel with preventDefault() (Legacy)
Educational only: in old Firefox, canceling the event could stop that static script.
JavaScript
document.addEventListener("beforescriptexecute", (e) => {
console.log("About to run:", e.target && e.target.id);
// Legacy Gecko only — could stop this static script:
// e.preventDefault();
console.log("Do not rely on cancel in modern browsers");
});
console.log("Cancel hook is literacy-only today");
onbeforescriptexecute in document: false
Use CSP / careful script loading instead
How It Works
Probe the handler property."onbeforescriptexecute" in document is a rough signal.
Branch safely. Only register the legacy listener when the property exists.
Fallback path. Otherwise print the portable advice: use CSP.
Still prefer modern APIs. Even if the property exists, engines may not dispatch the event.
Compatibility
Browser Support
beforescriptexecute is a Deprecated and Non-standard Gecko Document event (MDN). Other engines never implemented it; modern Firefox no longer dispatches it to web content.
✓ Deprecated · Non-standard
Document beforescriptexecute
Legacy Firefox-only static-script before-run event — do not build features on it.
LegacyNot for new apps
Google ChromeNever implemented
Unavailable
Mozilla FirefoxLegacy only · removed / disabled in modern versions
Avoid
Apple SafariNever implemented
Unavailable
Microsoft EdgeNever implemented (Chromium)
Unavailable
OperaNever implemented (Chromium)
Unavailable
Internet ExplorerNo beforescriptexecute support
Unavailable
beforescriptexecuteDeprecated
Bottom line: Recognize it in old Gecko code, then migrate. Prefer CSP and careful script loading. Remember: even historically, dynamically appended scripts did not fire this event.
Frequently Asked Questions
A proprietary Gecko (Firefox) Document event that fired when a static <script> was about to start executing. MDN marks it Deprecated and Non-standard. It was never a finished web standard.
No. MDN: it does not fire when a script element is added dynamically (for example with appendChild()). It only applied to static scripts in the markup in supporting legacy Firefox.
Yes, in legacy Gecko. Calling preventDefault() on beforescriptexecute could stop that static script from running. The sibling afterscriptexecute only notified after the script had already run.
No. Prefer Content Security Policy (CSP), avoid injecting untrusted scripts, and use ES modules with known imports. Do not rely on this Gecko-only cancel hook.
Historically only Firefox (Gecko). Chrome, Safari, and Edge never shipped it. Modern Firefox stopped dispatching it to web content.
In supporting legacy Firefox: document.addEventListener("beforescriptexecute", handler) or document.onbeforescriptexecute = handler. Always feature-detect; do not assume the event exists.