JavaScript Element beforescriptexecute Event

Beginner
5 min read
Updated: Nov 2026
3 examples
Event

Definition

In JavaScript, the beforescriptexecute event fired when a script was about to be executed. Canceling it prevented that script from running (MDN). It is a proprietary Gecko (Firefox) event — not for new apps.

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)
CallWhat it does
addEventListener("beforescriptexecute", fn)Primary signature.

Built into the browser. Listen with el.addEventListener("beforescriptexecute", …) — no import required.

Library declarations
JavaScript
addEventListener("beforescriptexecute", fn)

Parameters

ParameterDescription
Modern pathRequired. CSP / modules / no untrusted inject

Return Value

Return ValueDescription
*Fires as an Element event (listener receives the Event object).

JavaScript Examples

Example 1 — addEventListener("beforescriptexecute")

Listen and log when a script is about to run (legacy Firefox only).

JavaScript
function starting(e) {
  console.log("About to run:", e.target && e.target.nodeName);
}

document.addEventListener("beforescriptexecute", starting);
console.log("Listener attached (may never fire today)");
Try It Yourself

How It Works

  1. Define a handler. Logs the about-to-run script node when the event fires.
  2. Register on document. Same pattern as any DOM event via addEventListener.
  3. Expect silence today. Most browsers never dispatch this event.
  4. When it did fire. event.target was the <script> about to run.

Example 2 — Cancel with preventDefault() (Legacy)

Educational only: in old Firefox, canceling the event could stop that script.

JavaScript
document.addEventListener("beforescriptexecute", (e) => {
  console.log("About to run:", e.target && e.target.id);
  // Legacy Gecko only — could stop this script:
  // e.preventDefault();
  console.log("Do not rely on cancel in modern browsers");
});

console.log("Cancel hook is literacy-only today");
Try It Yourself

How It Works

  1. Listen before run. The handler would run before the script executed.
  2. Optional cancel. preventDefault() could block that script in legacy Gecko.
  3. Sibling difference. afterscriptexecute only notified after the script had already run.
  4. Use CSP instead. Portable script control belongs in Content Security Policy.

Example 3 — Feature Detection

Never assume the event exists. Check before you depend on it.

JavaScript
const supported = "onbeforescriptexecute" in document;
console.log("onbeforescriptexecute in document:", supported);

if (supported) {
  document.addEventListener("beforescriptexecute", () => {
    console.log("Legacy path: beforescriptexecute");
  });
} else {
  console.log("Use CSP / careful script loading instead");
}
Try It Yourself

How It Works

  1. Probe the handler property. "onbeforescriptexecute" in document is a rough signal.
  2. Branch safely. Only register the legacy listener when the property exists.
  3. Fallback path. Otherwise print the portable advice: use CSP.
  4. Still prefer modern APIs. Even if the property exists, engines may not dispatch the event.

Browser Support

Element.beforescriptexecute is a Deprecated and Non-standard Gecko event (MDN). Other engines never implemented it; modern Firefox no longer dispatches it to web content.

✓ Deprecated · Non-standard

Element beforescriptexecute

Legacy Firefox-only before-script event — do not build features on it.

Legacy Not for new apps
Google Chrome Never implemented
Unavailable
Mozilla Firefox Legacy only · removed / disabled in modern versions
Avoid
Apple Safari Never implemented
Unavailable
Microsoft Edge Never implemented (Chromium)
Unavailable
Opera Never implemented (Chromium)
Unavailable
Internet Explorer No beforescriptexecute support
Unavailable
beforescriptexecute Deprecated

Bottom line: Recognize it in old Gecko code, then migrate. Prefer CSP and careful script loading. See the MDN Element beforescriptexecute reference.

Frequently Asked Questions

A proprietary Gecko (Firefox) DOM event that fired when a script was about to execute. Canceling it prevented the script from running. MDN marks Element beforescriptexecute as Deprecated and Non-standard. It was never a finished web standard.
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.
Yes, in legacy Gecko. MDN: canceling 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.
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.

Next: Element beforexrselect

Learn the event fired before an XR session select action is processed.

beforexrselect tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful