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

load-script

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

load-script - npm Package Compare versions

Comparing version 0.0.5 to 1.0.0

36

index.js
module.exports = function load (src, cb) {
module.exports = function load (src, opts, cb) {
var head = document.head || document.getElementsByTagName('head')[0]
var script = document.createElement('script')
cb = cb || function() {};
if (typeof opts === 'function') {
cb = opts
opts = {}
}
script.type = 'text/javascript'
script.charset = 'utf8'
script.async = true
opts = opts || {}
cb = cb || function() {}
script.type = opts.type || 'text/javascript'
script.charset = opts.charset || 'utf8';
script.async = 'async' in opts ? !!opts.async : true
script.src = src
if (opts.attrs) {
setAttributes(script, opts.attrs)
}
if (opts.text) {
script.text = '' + opts.text
}
var onend = 'onload' in script ? stdOnEnd : ieOnEnd

@@ -26,6 +40,12 @@ onend(script, cb)

function setAttributes(script, attrs) {
for (var attr in attrs) {
script.setAttribute(attr, attrs[attr]);
}
}
function stdOnEnd (script, cb) {
script.onload = function () {
this.onerror = this.onload = null
cb()
cb(null, script)
}

@@ -36,3 +56,3 @@ script.onerror = function () {

this.onerror = this.onload = null
cb(new Error('Failed to load ' + this.src))
cb(new Error('Failed to load ' + this.src), script)
}

@@ -45,4 +65,4 @@ }

this.onreadystatechange = null
cb(null, true) // there is no way to catch loading errors in IE8
cb(null, script) // there is no way to catch loading errors in IE8
}
}
{
"name": "load-script",
"description": "Dynamic script loading for browser",
"version": "0.0.5",
"version": "1.0.0",
"keywords": [

@@ -19,5 +19,5 @@ "browser",

"devDependencies": {
"zuul": "~1.5.4"
"zuul": "~2.1.0"
},
"license": "MIT"
}

@@ -20,9 +20,32 @@ # load-script

## API
`load-script` appends a `script` node to the `<head>` element in the dom.
`require('load-script')` returns a function of the following interface: `function(url[, opts][, cb]) {}`
### url
Any url that you would like to load. May be absolute or relative.
### [, opts]
A map of options. Here are the currently supported options:
* `async` - A boolean value used for `script.async`. By default this is `true`.
* `attrs` - A map of attributes to set on the `script` node before appending it to the DOM. By default this is empty.
* `charset` - A string value used for `script.charset`. By default this is `utf8`.
* `text` - A string of text to append to the `script` node before it is appended to the DOM. By default this is empty.
* `type` - A string used for `script.type`. By default this is `text/javascript`.
### [, cb]
A callback function of the following interface: `function(err, script) {}` where `err` is an error if any occurred and `script` is the `script` node that was appended to the DOM.
## Example Usage
```javascript
var load = require('load-script')
load('foo.js', function (err) {
load('foo.js', function (err, script) {
if (err) {
// print useful message
}
else {
console.log(script.src);// Prints 'foo'.js'
// use script

@@ -29,0 +52,0 @@ // note that in IE8 and below loading error wouldn't be reported

@@ -18,2 +18,41 @@ var assert = require('assert');

test('opts.async', function(done) {
load('test/hello.js', {async: false}, function(err, script) {
assert.ifError(err);
assert.equal(script.async, false);
done();
})
});
test('opts.attrs', function(done) {
load('test/hello.js', {attrs: {foo: 'boo'}}, function(err, script) {
assert.ifError(err);
assert.equal(script.getAttribute('foo'), 'boo');
done();
})
});
test('opts.charset', function(done) {
load('test/hello.js', {charset: 'iso-8859-1'}, function(err, script) {
assert.ifError(err);
assert.equal(script.charset, 'iso-8859-1');
done();
})
});
test('opts.text', function(done) {
load('test/hello.js', {text: 'foo=5;'}, function(err, script) {
assert.ifError(err);
done();
})
});
test('opts.type', function(done) {
load('test/hello.js', {type: 'text/ecmascript'}, function(err, script) {
assert.ifError(err);
assert.equal(script.type, 'text/ecmascript');
done();
})
});
test('no exist', function(done) {

@@ -20,0 +59,0 @@ load('unexistent.js', function (err, legacy) {

Sorry, the diff of this file is not supported yet

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