README
@afuggini/loadscript
Loads external Javascript files asynchronously using promises.
Promisified version of https://www.npmjs.com/package/load-script
API
@afuggini/loadscript
appends a script
node to the <head>
element in the dom.
require('@fuggini/loadscript')
returns a function of the following interface: function(url: string | string[][, opts]): Promise<void> {}
url
Any url or array of urls to load. May be absolute or relative.
[, opts]
A map of options. Here are the currently supported options:
async
- A boolean value used forscript.async
. By default this istrue
.attrs
- A map of attributes to set on thescript
node before appending it to the DOM. By default this is empty.charset
- A string value used forscript.charset
. By default this isutf8
.text
- A string of text to append to thescript
node before it is appended to the DOM. By default this is empty.type
- A string used forscript.type
. By default this istext/javascript
.
Install
$ npm i @afuggini/loadscript
Example Usage
const loadScript = require('@afuggini/loadscript')
// Load one script
loadScript('example.js').then(...)
// Load multiple scripts. Promise returns when all of them are ready.
loadScript(['//somesite.com/script.js', 'http://anotherweb.com/another/file.js']).then(...)
License
MIT