load-script
Dynamic browser script loading with a simple callback-based API.
Repository Health
Technical Analysis
load-script is a minimal browser utility for loading external JavaScript files at runtime. It appends a script node to the document head and invokes a callback when the script finishes loading or fails, giving you programmatic control over on-demand code loading.
With support for async toggling, custom attributes, charset, and inline text, it wraps the boilerplate of DOM script injection — including legacy IE readystate handling — behind a single function call.
What You Get
- A single
load(url, opts, cb)function for dynamic script injection - Callback notification on both successful load and load failure
- Options for async, type, charset, custom attributes, and inline text
- Cross-browser onload handling including legacy IE readystatechange fallback
Common Use Cases
- Lazy-loading third-party widgets or SDKs only when needed
- Deferring non-critical scripts to improve initial page load
- Conditionally injecting analytics or payment provider scripts at runtime
Under The Hood
Architecture - The library is a single index.js module exporting one function. It grabs document.head, creates a script element, applies options (type, charset, async, custom attrs, inline text), attaches onload/onerror handlers via a stdOnEnd/ieOnEnd split for cross-browser completion detection, then appends the node to the head. Tech Stack - Plain ES5 JavaScript with zero runtime dependencies; ships a hand-written index.d.ts for TypeScript consumers. Tests run through zuul. Code Quality - Compact and readable at under 60 lines, with a browser test suite in test/. The IE fallback paths add legacy complexity but are clearly commented. API Design - Minimal and ergonomic: one function, optional options and callback with sensible defaults, so the simplest call is load(url, cb).
Used by 2 apps in this directory
Tianji
Analytics · Monitoring
Replace Google Analytics, UptimeKuma, and Prometheus with one self-hosted platform that tracks websites, monitors uptime, and reports server health.
Webiny JS
Ecommerce · Blogging · CMS
Open-source, self-hosted CMS on AWS serverless — a TypeScript framework you extend with code, not a product you configure through a UI.