All Projects → tomek-f → simple-load-script

tomek-f / simple-load-script

Licence: MIT license
Very simple promise based script loader and JSONP

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to simple-load-script

Discord.js Musicbot Addon
This DOES NOT WORK any more. This repo only serves as an archive for is anyone wants to pickup my work. You may still join the discord however.
Stars: ✭ 109 (+738.46%)
Mutual labels:  npm-module
Reactopt
A CLI React performance optimization tool that identifies potential unnecessary re-rendering
Stars: ✭ 1,975 (+15092.31%)
Mutual labels:  npm-module
Ts ci
✅ Continuous integration setup for TypeScript projects via GitHub Actions.
Stars: ✭ 225 (+1630.77%)
Mutual labels:  npm-module
React Ckeditor
CKEditor component for React with plugin and custom event listeners support
Stars: ✭ 124 (+853.85%)
Mutual labels:  npm-module
Homebridge Wol
A Wake on Lan plugin for Homebridge
Stars: ✭ 150 (+1053.85%)
Mutual labels:  npm-module
Darkmode.js
🌓 Add a dark-mode / night-mode to your website in a few seconds
Stars: ✭ 2,339 (+17892.31%)
Mutual labels:  npm-module
Easy Soap Request
Small Node.js library to make SOAP requests easier
Stars: ✭ 99 (+661.54%)
Mutual labels:  npm-module
ifto
A simple debugging module for AWS Lambda (λ) timeout
Stars: ✭ 72 (+453.85%)
Mutual labels:  npm-module
Cerebral
Declarative state and side effects management for popular JavaScript frameworks
Stars: ✭ 1,946 (+14869.23%)
Mutual labels:  npm-module
Eslint Plugin Eslint Comments
Additional ESLint rules for directive comments of ESLint.
Stars: ✭ 221 (+1600%)
Mutual labels:  npm-module
Zoom
Javascript library to do pinch zoom that preserves scale and rotation correctly.
Stars: ✭ 130 (+900%)
Mutual labels:  npm-module
Tanam
Plug-n-play CMS for websites on Firebase
Stars: ✭ 139 (+969.23%)
Mutual labels:  npm-module
Jsonexport
{} → 📄 it's easy to convert JSON to CSV
Stars: ✭ 208 (+1500%)
Mutual labels:  npm-module
Tspath
TypeScript path alias resolver
Stars: ✭ 115 (+784.62%)
Mutual labels:  npm-module
Singlespotify
🎵 Create Spotify playlists based on one artist through the command line
Stars: ✭ 254 (+1853.85%)
Mutual labels:  npm-module
Nekos Dot Life
Nekos.life wrapper.
Stars: ✭ 108 (+730.77%)
Mutual labels:  npm-module
Node Regedit
Read, Write, List and do all sorts of funky stuff to the windows registry using node.js and windows script host
Stars: ✭ 178 (+1269.23%)
Mutual labels:  npm-module
fetch-action-creator
Fetches using standardized, four-part asynchronous actions for redux-thunk.
Stars: ✭ 28 (+115.38%)
Mutual labels:  npm-module
pwainit
Turn your existing website to Progressive Web App or Initiate PWA project using single command!!. 'npm i -g pwainit' to install pwainit and get started 🚀
Stars: ✭ 45 (+246.15%)
Mutual labels:  npm-module
Abort Controller
An implementation of WHATWG AbortController interface.
Stars: ✭ 213 (+1538.46%)
Mutual labels:  npm-module

simple-load-script

Very simple promise based script loader and JSONP

Installation

npm install --save simple-load-script

Import

// es5 CommonJS
const loadScript = require('simple-load-script');

// es6
const loadScript = require('simple-load-script/es6/simpleLoadScript');

// es5-umd
const loadScript = require('simple-load-script/es5-umd/simpleLoadScript');

Usage

import loadScript from 'simple-load-script';

loadScript('//code.jquery.com/jquery-2.2.3.js')
  .then(function (scriptRef) {
    console.log('success', scriptRef);
  })
  .catch(function (err) {
    console.log(err);
  });
import loadScript from 'simple-load-script';

try {
  /* const scriptRef = */ loadScript('//code.jquery.com/jquery-2.2.3.js');
} catch (err) {
  console.log(err);
}
import loadScript from 'simple-load-script';

loadScript('//code.jquery.com/jquery-2.2.3.js', {
  inBody: true,
})
  .then(function (scriptRef) {
    console.log('success', scriptRef); // 'success', script ref.
  })
  .catch(function (err) {
    console.log(err);
  });
import loadScript from 'simple-load-script';

loadScript({
  url: '//code.jquery.com/jquery-2.2.3.js',
  attrs: { id: 'one', charset: 'UTF-8' },
})
  .then(function (scriptRef) {
    console.log('success', scriptRef); // 'success', script ref.
  })
  .catch(function (err) {
    console.log(err);
  });

Google Maps API

import loadScript from 'simple-load-script';

loadScript({
  url: '//maps.googleapis.com/maps/api/js?&callback=gmapiready',
  callBackName: 'gmapiready',
})
  .then(function (scriptRef) {
    console.log('success', scriptRef); // 'success', undefined
  })
  .catch(function (err) {
    console.log(err);
  });

JSONP

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

loadScripts('//api.ipinfodb.com/v3/ip-city/?format=json&callback=elo', {
  callBackName: 'elo',
  removeScript: true,
})
  .then(function (scriptRef) {
    console.log('success', scriptRef); // 'success', res
  })
  .catch(function (err) {
    console.log(err);
  });

Load more scripts (Promise.all) - urls

import loadScript from 'simple-load-script';

loadScripts(
  '//example.com/test1.js',
  '//example.com/test2.js',
  '//example.com/test3.js',
)
  .then(function (scriptRef) {
    console.log('success', scriptRef); // 'success', res
  })
  .catch(function (err) {
    console.log(err);
  });

Load more scripts (Promise.all) - objects and urls, callBackNames must be unique names

import loadScript from 'simple-load-script';

loadScripts(
  {
    url: '//maps.googleapis.com/maps/api/js?&callback=gmapiready',
    callBackName: 'gmapiready',
  },
  {
    url: '//api.ipinfodb.com/v3/ip-city/?format=json&callback=elo',
    callBackName: 'elo',
    removeScript: true,
  },
  [
    'https://api.twitter.com/1/statuses/oembed.json?id=507185938620219395&callback=elo2',
    { callBackName: 'elo2' },
  ],
  '//code.jquery.com/jquery-2.2.3.js',
)
  .then(function (scriptRef) {
    console.log('success', scriptRef); // 'success', array
  })
  .catch(function (err) {
    console.log(err);
  });

Arguments

  • url (optional) - file to append to body
  • options (required) - options in object

Options

  • url (string) - file to append to body
  • inBody (boolean) - append to document.body instead of document.head
  • attrs (object) - with attributes to append to script tag (charset, type, id, …)
  • callBackName (string) - callback to add to window object; promise is resolved after callback is fired; callback is removed after that; multiple callbacks must have unique names
  • dontRemoveCallBack (boolean) - from window after load; no real use - let me know
  • removeScript (boolean) - after load (for JSONP, other reasons); it's always removed on error
  • insertInto (string) - CSS selector (an ID, class name, element name, etc.) to insert the script into. Overrides inBody value.

Returned values

  • then: resource (JSONP - options.callBackName) script reference in DOM or undefined (options.callBackName, options.removeScript)
  • catch: error message

Changelog

View on github.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].