JavaScript Element afterscriptexecute Event

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Deprecated
Non-standard
Instance event

What You’ll Learn

The afterscriptexecute event once fired after a script finished running in Firefox. Learn what it meant, why it is deprecated and non-standard, how to feature-detect it safely, how it differed from beforescriptexecute, and which portable APIs to use instead—with five examples and try-it labs.

01

Kind

Instance event

02

Type

Event

03

Cancelable

No

04

Status

Deprecated · Non-standard

05

Engine

Legacy Gecko only

06

Modern path

script.onload

Introduction

Sometimes you want to know when a <script> has finished executing—for logging, analytics, or coordinating code that depends on that script. Early Gecko experiments added a pair of proprietary events: beforescriptexecute (before run) and afterscriptexecute (after run).

afterscriptexecute answered a simple question: did this script just finish? It was never adopted by other browsers, never finished as a standard, and MDN warns not to rely on it.

⚠️
Deprecated & non-standard

Learn this event for legacy Firefox code and interviews. For new apps, use standard script lifecycle hooks (load / error on the script element) or ES modules instead of Gecko-only events.

Understanding afterscriptexecute

An instance event on Element (listened on document or other nodes in supporting Gecko). It notified you after script execution completed.

  • Fires after a script has been executed (legacy Firefox).
  • Not cancelable — you cannot undo execution after the fact.
  • Generic Event — no special payload properties.
  • Siblingbeforescriptexecute ran earlier and could cancel execution.
  • Status — Deprecated and Non-standard on MDN; not part of any specification.

📝 Syntax

Use the event name with addEventListener, or set the handler property (legacy Firefox only):

JavaScript
addEventListener("afterscriptexecute", (event) => { });

onafterscriptexecute = (event) => { };

Event type

A generic Event.

Cancelable

MDN: this event is not cancelable. Calling preventDefault() does not undo a script that already ran.

⚖️ afterscriptexecute vs beforescriptexecute vs load

APIWhen it firesPortable?
beforescriptexecuteScript about to run (could cancel)No — legacy Gecko only
afterscriptexecuteScript just finished runningNo — legacy Gecko only
script loadExternal classic script finished loading/executingYes — use this
script errorScript failed to loadYes — use this

🚀 What to Use Instead

For an external classic script, attach standard listeners to the script element itself:

JavaScript
const script = document.createElement("script");
script.src = "/libs/helper.js";

script.addEventListener("load", () => {
  console.log("Script loaded and executed");
});

script.addEventListener("error", () => {
  console.error("Script failed to load");
});

document.head.appendChild(script);

For modules, use <script type="module"> and normal import / top-level await so dependencies are explicit. To watch scripts being inserted into the DOM, use MutationObserver—not proprietary execute events.

⚡ Quick Reference

GoalCode / note
Listen (legacy)document.addEventListener("afterscriptexecute", fn)
Handler propertydocument.onafterscriptexecute = fn
Cancelable?No
Event typeGeneric Event
Feature-detect"onafterscriptexecute" in document (weak; still prefer alternatives)
Modern replacescript.addEventListener("load", fn)
MDN statusDeprecated · Non-standard

🔍 At a Glance

Four facts to remember about afterscriptexecute.

Event type
Event

Generic

Cancelable
no

After the fact

Standard?
no

Non-standard

New code?
avoid

Use script load

Examples Gallery

Examples follow MDN Element: afterscriptexecute 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("afterscriptexecute")

MDN style: listen on document and log when a script finishes (where supported).

JavaScript
document.addEventListener("afterscriptexecute", (event) => {
  console.log("afterscriptexecute 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.
Try It Yourself

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 — onafterscriptexecute Property

MDN’s alternate style using the event handler property.

JavaScript
document.onafterscriptexecute = (event) => {
  console.log("Script finished (onafterscriptexecute)");
};

// Assigning again replaces the previous handler.
// Prefer addEventListener when you need multiple listeners.
Try It Yourself

How It Works

Same event, different registration API. One property means one handler—easy to overwrite by accident.

📈 Detect, Pair & Replace

Feature-detect safely, compare with the sibling event, and migrate.

Example 3 — Feature Detection

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

JavaScript
const supported = "onafterscriptexecute" in document;

console.log("onafterscriptexecute in document:", supported);

if (supported) {
  document.addEventListener("afterscriptexecute", () => {
    console.log("Legacy path: afterscriptexecute");
  });
} else {
  console.log("Use script load/error events instead");
}
Try It Yourself

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 APIs.

Example 4 — Pair with beforescriptexecute

In old Gecko, before/after formed a bracket around script execution.

JavaScript
document.addEventListener("beforescriptexecute", () => {
  console.log("1) before script execute");
});

document.addEventListener("afterscriptexecute", () => {
  console.log("2) after script execute");
});

// Historical order in supporting Firefox:
// beforescriptexecute → script runs → afterscriptexecute
// Both events are deprecated and non-standard.
Try It Yourself

How It Works

beforescriptexecute could cancel execution; afterscriptexecute only reported completion. Both belong in the history books for production apps.

Example 5 — Modern Replacement with load

Portable pattern: listen on the script element you create.

JavaScript
const script = document.createElement("script");
script.textContent = "window.__demoRan = true;";

script.addEventListener("load", () => {
  // Note: inline scripts may not fire load the same way as external ones.
  console.log("load fired (external scripts are the usual case)");
});

// External example pattern:
// script.src = "https://example.com/lib.js";
// script.addEventListener("load", () => console.log("executed"));
// script.addEventListener("error", () => console.error("failed"));
// document.head.appendChild(script);

console.log("Prefer load/error over afterscriptexecute");
console.log("demo flag set via inline script:", typeof window.__demoRan !== "undefined");
document.body.appendChild(script);
console.log("after append, __demoRan:", window.__demoRan === true);
Try It Yourself

How It Works

Inline scripts run as soon as they are inserted. External scripts expose a reliable load event when fetch + execute succeed. That is the cross-browser replacement for proprietary after-execute notifications.

🚀 Common Use Cases

  • Understanding legacy Firefox / Gecko code that monitored script execution.
  • 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 load / error or ES modules for real production work.

🔧 How It Works

1

Script is about to run

Browser prepares to execute a <script> element.

Prepare
2

Script executes

JavaScript runs to completion (or throws).

Run
3

afterscriptexecute (legacy)

Old Gecko dispatched a non-cancelable Event.

Notify
4

Prefer standard load hooks

Use load / error or modules in new code.

📝 Notes

  • MDN: Deprecated and Non-standard — avoid in production.
  • Not cancelable; sibling beforescriptexecute was the cancelable one.
  • Proprietary to Gecko; never a cross-browser API.
  • Modern Firefox stopped shipping useful web-facing support for these events.
  • Related learning: addEventListener(), toggleAttribute(), tagName, JavaScript hub.

Very Limited / Legacy Browser Support

afterscriptexecute 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 script load/error events.

Deprecated · Non-standard

Element afterscriptexecute

Do not build features on this event. Use it only to understand or migrate legacy Firefox code.

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 afterscriptexecute support
Unavailable
afterscriptexecute Deprecated

Bottom line: Feature-detect if you must touch legacy Gecko code. For new work, use script load/error events or ES modules.

Conclusion

afterscriptexecute once told Firefox pages that a script had finished running. Today it is a deprecated, non-standard footnote: useful for reading old code, not for shipping new products.

Continue with animationcancel, toggleAttribute(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Prefer script load / error for external scripts
  • Use ES modules when dependency order matters
  • Feature-detect before touching legacy Gecko-only APIs
  • Document why any remaining legacy listener exists
  • Plan migration off proprietary script-execute events

❌ Don’t

  • Build new features on afterscriptexecute
  • Assume Chrome, Safari, or Edge ever supported it
  • Expect preventDefault() to undo execution
  • Confuse it with the standard load event
  • Treat non-standard Gecko events as future-proof

Key Takeaways

Knowledge Unlocked

Five things to remember about afterscriptexecute

Legacy Gecko “script finished” signal — prefer standard load hooks now.

5
Core concepts
⚠️ 02

Deprecated

avoid new use

Status
🚫 03

Non-standard

Gecko only

Compat
🚫 04

Not cancelable

after the fact

API
🚀 05

Replace

script load

Modern

❓ Frequently Asked Questions

It is a proprietary Gecko (Firefox) DOM event that fired after a script element had finished executing. MDN marks it Deprecated and Non-standard. It was never a finished web standard.
No. Prefer portable APIs such as script.onload / script.onerror for classic scripts, the load event on the script element, module scripts with type="module", or MutationObserver when you need to watch scripts being inserted.
No. MDN states the event is not cancelable. Its sibling beforescriptexecute could cancel script execution in old Firefox; afterscriptexecute only notified after the fact.
Historically only Firefox (Gecko). Other major browsers never shipped it. Modern Firefox stopped dispatching it to web content (around Firefox 144) and later removed the implementation.
In supporting legacy Firefox: document.addEventListener("afterscriptexecute", handler) or document.onafterscriptexecute = 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.

Next: animationcancel

Learn when a CSS animation aborts without firing animationend.

animationcancel →

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.

5 people found this page helpful