All Projects → feross → load-script2

feross / load-script2

Licence: MIT license
Dynamic script loading for modern browsers

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to load-script2

Labjs
Loading And Blocking JavaScript: On-demand parallel loader for JavaScript with execution order dependencies
Stars: ✭ 2,277 (+4850%)
Mutual labels:  script-loader
indexed-cache
A tiny Javsacript library for sideloading static assets on pages and caching them in the browser's IndexedDB for longer-term storage.
Stars: ✭ 56 (+21.74%)
Mutual labels:  script-loader
micro-requirejs
⛔️ [DEPRECATED] Tiny asynchronous dependency loader, that does not require modification of dependent scripts
Stars: ✭ 22 (-52.17%)
Mutual labels:  script-loader
async
A lightweight and high performance async CSS and script loader for frontend optimization.
Stars: ✭ 17 (-63.04%)
Mutual labels:  script-loader

load-script2 travis npm downloads javascript style guide

Dynamic script loading for modern browsers

Works in the browser with browserify!

install

npm install load-script2

This package works in the browser with browserify. If you do not use a bundler, you can use the standalone script directly in a <script> tag.

usage

const loadScript = require('load-script2')

const script = await loadScript('foo.js')
console.log(script.src);// Prints 'foo'.js'

differences to load-script

load-script2 does not support legacy browsers like IE8 because these browsers do not have standards-based DOM APIs. load-script2 also removes many esoteric options, which aren't needed most of the time and adds promises support.

The size of load-script2 is 509 bytes, compared to 655 bytes for load-script (minified and gzipped).

API

promise = loadScript(src, [attrs], [parentNode])

Append a <script> node with the given src URL to the <head> element in the DOM.

src

Any url that you would like to load. May be absolute or relative.

attrs (optional)

An object that contains HTML attributes to set on the <script> tag. For example, the value { id: 'hi' } would set the attribute id="hi" on the <script> tag before it is injected.

parentNode (optional)

The HTML node to which the <script> tag will be appended. If not specified, defaults to the <head> tag.

promise

Returns a promise which resolves to the script node that was appended to the DOM, or rejects with err if any occurred.

license

MIT. Copyright (c) Feross Aboukhadijeh.

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