The beforescriptexecute event once fired before a script ran in Firefox. Cancelling it could stop execution. Learn why it is deprecated and non-standard, how to feature-detect it, how it differed from afterscriptexecute, and which portable approaches to use instead—with five examples and try-it labs.
01
Kind
Instance event
02
Type
Event
03
Cancelable
Yes (legacy)
04
Status
Deprecated · Non-standard
05
Engine
Legacy Gecko only
06
Modern path
CSP / safe loading
Fundamentals
Introduction
Early Gecko experiments added a pair of proprietary events around script execution: beforescriptexecute (before run) and afterscriptexecute (after run). The “before” event was interesting because cancelling it could block the script.
That idea never became a web standard. Other browsers never shipped it, MDN warns not to rely on it, and modern Firefox stopped exposing useful web-facing support.
⚠️
Deprecated & non-standard
Learn this event for legacy Firefox code and interviews. For new apps, control scripts with CSP, trusted sources, modules, and careful DOM insertion—not Gecko-only events.
Concept
Understanding beforescriptexecute
An instance event on Element (listened on document or other nodes in supporting Gecko). It notified you that a script was about to execute.
Fires when a script is about to be executed (legacy Firefox).
Cancelable — preventDefault() could stop execution.
Generic Event — no special payload properties.
Sibling — afterscriptexecute ran later and was not cancelable.
Status — Deprecated and Non-standard on MDN; not part of any specification.
Foundation
📝 Syntax
Use the event name with addEventListener, or set the handler property (legacy Firefox only):
MDN: cancelling the event prevents the script from executing (in engines that still supported this behavior). Call event.preventDefault() inside the handler.
Compare
⚖️ beforescriptexecute vs afterscriptexecute vs CSP
API
When it fires
Portable?
beforescriptexecute
Script about to run (could cancel)
No — legacy Gecko only
afterscriptexecute
Script just finished running
No — legacy Gecko only
Content Security Policy
Browser enforces which scripts may run
Yes — prefer this
scriptload / error
External script finished or failed
Yes — after-run hooks
Replacement
🚀 What to Use Instead
There is no portable DOM event that cancels arbitrary scripts the way legacy beforescriptexecute did. Use layered, standard approaches:
CSP — declare which script sources (and inline policies) are allowed.
Do not inject untrusted scripts — fix the source, not the symptom.
ES modules — explicit imports instead of ad-hoc script tags.
MutationObserver — some legacy blockers changed a new script’s type before it ran (fragile; CSP is better).
JavaScript
// Educational pattern only — prefer CSP in real apps.
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeName === "SCRIPT") {
node.type = "text/plain"; // stops it from executing as JS
console.log("Neutralized an injected script (demo)");
}
}
}
});
observer.observe(document.documentElement, { childList: true, subtree: true });
"onbeforescriptexecute" in document (weak; still prefer alternatives)
Modern replace
CSP + safe script loading (not a single event)
MDN status
Deprecated · Non-standard
Snapshot
🔍 At a Glance
Four facts to remember about beforescriptexecute.
Event type
Event
Generic
Cancelable
yes
Legacy Gecko
Standard?
no
Non-standard
New code?
avoid
Use CSP
Hands-On
Examples Gallery
Examples follow MDN Element: beforescriptexecute event and show safe feature detection plus modern replacements. In most current browsers the proprietary event will never fire—the labs still teach the patterns clearly.
📚 Getting Started
Register handlers the MDN ways (legacy Gecko).
Example 1 — addEventListener("beforescriptexecute")
MDN style: listen on document and log when a script is about to run (where supported).
JavaScript
document.addEventListener("beforescriptexecute", (event) => {
console.log("beforescriptexecute fired");
console.log("target:", event.target && event.target.nodeName);
});
// In legacy Firefox, inserting/running a <script> could fire this.
// In modern browsers it typically never fires.
Listener attached (event may never fire in modern browsers)
How It Works
You register the same way as any other DOM event. Support is the hard part: without legacy Gecko behavior, the handler sits idle.
Example 2 — onbeforescriptexecute Property
MDN’s alternate style using the event handler property.
JavaScript
document.onbeforescriptexecute = (event) => {
console.log("Script about to run (onbeforescriptexecute)");
};
// Assigning again replaces the previous handler.
// Prefer addEventListener when you need multiple listeners.
Same event, different registration API. One property means one handler—easy to overwrite by accident.
📈 Cancel, Detect & Replace
Show the legacy cancel path, feature-detect safely, and migrate.
Example 3 — Cancel with preventDefault()
MDN: cancelling the event prevented the script from executing (legacy Gecko).
JavaScript
document.addEventListener("beforescriptexecute", (event) => {
console.log("Blocking script execution (legacy only)");
event.preventDefault();
});
// Historical Gecko: script does not run after preventDefault().
// Modern browsers: this listener usually never fires.
onbeforescriptexecute in document: false
Use CSP / safe script loading instead
How It Works
Presence of the handler property is a rough signal. Even when the property exists, modern engines may not dispatch the event—so still prefer portable controls.
Example 5 — Portable Demo with MutationObserver
Educational stand-in: neutralize an injected script by changing its type.
JavaScript
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeName === "SCRIPT" && node.dataset.demo === "block") {
node.type = "text/plain";
console.log("Neutralized demo script");
}
}
}
});
observer.observe(document.documentElement, { childList: true, subtree: true });
const s = document.createElement("script");
s.dataset.demo = "block";
s.textContent = "window.__shouldStayUndefined = true;";
document.body.appendChild(s);
console.log(
"__shouldStayUndefined:",
typeof window.__shouldStayUndefined
);
// Prefer CSP in production; this is a teaching demo only.
Changing type away from JavaScript stops execution for that inserted node. Real apps should still rely on CSP and trusted script sources rather than racing the DOM.
Applications
🚀 Common Use Cases
Understanding legacy Firefox / Gecko code that tried to block scripts.
Migrating old extensions or pages off proprietary script-execute events.
Interview / history knowledge of early HTML script lifecycle proposals.
Teaching why feature detection and standards matter for DOM events.
Choosing CSP and safe loading strategies for real production work.
Under the Hood
🔧 How It Works
1
Script is about to run
Browser prepares to execute a <script> element.
Prepare
2
beforescriptexecute (legacy)
Old Gecko dispatched a cancelable Event.
Gate
3
Run or skip
If not canceled, the script executes; then afterscriptexecute could fire.
Decide
4
✓
Prefer CSP today
Control scripts with standards, not proprietary cancel events.
Important
📝 Notes
MDN: Deprecated and Non-standard — avoid in production.
Cancelable in legacy Gecko; sibling afterscriptexecute was not.
Proprietary to Gecko; never a cross-browser API.
Modern Firefox stopped shipping useful web-facing support for these events.
beforescriptexecute is a deprecated, non-standard Gecko event. Logos use the shared browser-image-sprite.png sprite from this project. Other engines never implemented it; modern Firefox no longer dispatches it to web content. Prefer CSP and safe script loading.
✓ Deprecated · Non-standard
Element beforescriptexecute
Do not build features on this event. Use it only to understand or migrate legacy Firefox code.
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: Feature-detect if you must touch legacy Gecko code. For new work, use CSP, modules, and trusted script sources.
Wrap Up
Conclusion
beforescriptexecute once let Firefox pages cancel a script before it ran. Today it is a deprecated, non-standard footnote: useful for reading old code, not for shipping new products.
Feature-detect before touching legacy Gecko-only APIs
Plan migration off proprietary script-execute events
❌ Don’t
Build new features on beforescriptexecute
Assume Chrome, Safari, or Edge ever supported it
Confuse cancelable before with non-cancelable after
Treat MutationObserver demos as a full CSP replacement
Treat non-standard Gecko events as future-proof
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about beforescriptexecute
Legacy Gecko “script about to run” gate — prefer CSP and safe loading now.
5
Core concepts
⏱️01
Before script runs
legacy gate
Event
⚠️02
Deprecated
avoid new use
Status
🚫03
Non-standard
Gecko only
Compat
🛑04
Was cancelable
preventDefault
API
🚀05
Replace
CSP / safe load
Modern
❓ Frequently Asked Questions
It is a proprietary Gecko (Firefox) DOM event that fired when a script was about to execute. Cancelling it prevented the script from running. MDN marks it Deprecated and Non-standard. It was never a finished web standard.
No. Prefer Content Security Policy (CSP), avoid injecting untrusted scripts, ES modules with known imports, or MutationObserver-based hardening. Do not rely on this Gecko-only cancel hook.
Yes, in legacy Gecko. MDN states cancelling the event prevented the script from executing. Its sibling afterscriptexecute was not cancelable — it only notified after the script had already run.
Historically only Firefox (Gecko). Other major browsers never shipped it. Modern Firefox stopped dispatching it to web content and later removed the implementation.
In supporting legacy Firefox: document.addEventListener("beforescriptexecute", handler) or document.onbeforescriptexecute = handler. Always feature-detect; do not assume the event exists.
A generic Event. There is no special payload beyond a normal Event object.
Did you know?
Mozilla later unshipped beforescriptexecute and afterscriptexecute from web content because they were non-standard and unused by other browsers. Script blockers that once relied on them moved to portable techniques such as changing a script’s type via MutationObserver—and, better yet, Content Security Policy.