@automattic/load-script

1.0.0 • Public • Published

Async Script Loader

This utility function allows us to use a standardized method of loading remote scripts and injecting them into the <head> of our document to bring external functionality into the app.

Usage

import { loadScript, loadjQueryDependentScript } from '@automattic/load-script';
loadScript( REMOTE_SCRIPT_URL, function( error ) {
	if ( error ) {
		debug( 'Script ' + error.src + ' failed to load.' );
		return;
	}
	debug( 'Script loaded!' );
} );

// if we need jQuery, this function will load it (if it's not loaded already)
loadjQueryDependentScript( REMOTE_SCRIPT_URL, function( error ) {
	if ( error ) {
		debug( 'Script ' + error.src + ' failed to load.' );
		return;
	}
	debug( 'Script and jQuery are loaded!' );
} );

Error handling

The callback should expect a single argument, which will be null on success or an object on failure. The object contains the src property, which will contain the src url of the script that failed to load.

/@automattic/load-script/

    Package Sidebar

    Install

    npm i @automattic/load-script

    Weekly Downloads

    2,573

    Version

    1.0.0

    License

    GPL-2.0-or-later

    Unpacked Size

    44.9 kB

    Total Files

    15

    Last publish

    Collaborators

    • macbre
    • yuliyan
    • mjangda
    • matticbot
    • a8c
    • blowery
    • ehg_
    • sgomes
    • tyxla
    • saroshaga
    • nejclovrencic
    • sirbrillig
    • chriszarate
    • robersongomes
    • johngodley
    • jeherve
    • daledupreez-a8c
    • t2dw4t
    • luismulinari
    • andrea-sdl
    • elazzabi
    • fmfernandes
    • sirreal
    • wwa
    • chihsuan
    • manzoorwanijk
    • msurdi-a8c
    • newspack
    • dsmartg
    • kthai15
    • bgrgicak
    • robertsreberski_a8c
    • artpi
    • gmjuhasz
    • kat3samsin
    • brunobasto
    • dhenridev
    • micbosia8c
    • mrmurphy
    • wpvip-bot
    • etobiesen
    • alshakero
    • arthur791004
    • dilirity
    • mattwest-a8c
    • mehmoodak
    • natalia.vidal
    • chriskmnds
    • oandregal
    • galatanovidiu