Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-load-script

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-load-script

Very simple promise based script and JSONP

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

simple-load-script

Very simple promise based script loader and JSONP

Usage

Async/await

import simpleLoadScript from 'simple-load-script';

try {
  const scriptRef = await simpleLoadScript('//code.jquery.com/jquery-2.2.3.js');

  console.log(scriptRef); // HTMLScriptElement
} catch (err) {
  console.log(err);
}

Promise

import simpleLoadScript from 'simple-load-script';

simpleLoadScript('//code.jquery.com/jquery-2.2.3.js')
  .then(function (scriptRef) {
    console.log(scriptRef);
  })
  .catch(function (err) {
    console.log(err);
  });

Config object

import simpleLoadScript from 'simple-load-script';

try {
  const scriptRef = await simpleLoadScript({
    url: '//code.jquery.com/jquery-2.2.3.js',
    inBody: true,
    attrs: { id: 'one', charset: 'UTF-8' },
  });

  console.log(scriptRef); // HTMLScriptElement inBody with attrs present
} catch (err) {
  console.log(err);
}

Google Maps API

Runs global callback (window.gmapiready)

import simpleLoadScript from 'simple-load-script';

try {
  const scriptRef = await simpleLoadScript('//maps.googleapis.com/maps/api/js?&callback=gmapiready');

  console.log(scriptRef); // HTMLScriptElement
} catch (err) {
  console.log(err);
}

JSONP

Runs global callback (window.elo)

var simpleLoadScript = require('simple-load-script');

try {
  const scriptRef = await simpleLoadScript({
    url: '//api.ipinfodb.com/v3/ip-city/?format=json&callback=elo',
    removeScript: true,
  });

  console.log(scriptRef); // undefined
} catch (err) {
  console.log(err);
}

Array mode - objects and urls, callBackNames must have unique names

import simpleLoadScript from 'simple-load-script';

try {
  const scriptRefs = await simpleLoadScript([
    '//maps.googleapis.com/maps/api/js?&callback=gmapiready',
    {
      url: '//api.ipinfodb.com/v3/ip-city/?format=json&callback=elo',
      removeScript: true,
    },
    '//code.jquery.com/jquery-2.2.3.js',
  ]);

  console.log(scriptRefs); // HTMLScriptElement[]
} catch (err) {
  console.log(err);
}

Arguments

Config | string | (Config | string)[]

Config

Interface

interface Config {
  url: string;
  attrs?: Record<string, string>;
  inBody?: boolean;
  insertInto?: string | null;
  removeScript?: boolean;
}

Default values

const defaultConfig = {
  url: '',
  attrs: {},
  inBody: false,
  insertInto: null,
  removeScript: false,
};
  • url - file to append to body
  • attrs - with attributes to append to script tag (charset, type, id, …)
  • inBody - append to document.body instead of document.head
  • insertInto - CSS selector (an ID, class name, element name, etc.) to insert the script into. Overrides inBody value.
  • removeScript - remove script tag after load; it's always removed on an error

Specific import

Check files or package.json

Changelog

View on github.

Misc.

  • uses addEventListener, Array.isArray, for…of, destructuring Promise & Promise.all

Keywords

FAQs

Package last updated on 09 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc