Skip to content

Commit c8a2155

Browse files
committed
Merge pull request #9 from jsdevel/adding-opts
Adding opts.attrs and opts.text.
2 parents 9c5910e + d200c86 commit c8a2155

4 files changed

Lines changed: 92 additions & 9 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
components
22
build
3+
node_modules

Readme.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,36 @@ $ npm install load-script
1717
```
1818

1919
## API
20+
`load-script` appends a `script` node to the `<head>` element in the dom.
21+
22+
`require('load-script')` returns a function of the following interface: `function(url[, opts][, cb]) {}`
23+
24+
### url
25+
Any url that you would like to load. May be absolute or relative.
26+
27+
### [, opts]
28+
A map of options. Here are the currently supported options:
29+
30+
* `async` - A boolean value used for `script.async`. By default this is `true`.
31+
* `attrs` - A map of attributes to set on the `script` node before appending it to the DOM. By default this is empty.
32+
* `charset` - A string value used for `script.charset`. By default this is `utf8`.
33+
* `text` - A string of text to append to the `script` node before it is appended to the DOM. By default this is empty.
34+
* `type` - A string used for `script.type`. By default this is `text/javascript`.
35+
36+
### [, cb]
37+
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.
38+
39+
## Example Usage
2040

2141
```javascript
2242
var load = require('load-script')
2343

24-
load('foo.js', function (err) {
44+
load('foo.js', function (err, script) {
2545
if (err) {
2646
// print useful message
2747
}
2848
else {
49+
console.log(script.src);// Prints 'foo'.js'
2950
// use script
3051
// note that in IE8 and below loading error wouldn't be reported
3152
}

index.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11

2-
module.exports = function load (src, cb) {
2+
module.exports = function load (src, opts, cb) {
33
var head = document.head || document.getElementsByTagName('head')[0]
44
var script = document.createElement('script')
55

6-
cb = cb || function() {};
6+
if (typeof opts === 'function') {
7+
cb = opts
8+
opts = {}
9+
}
10+
11+
opts = opts || {}
12+
cb = cb || function() {}
713

8-
script.type = 'text/javascript'
9-
script.charset = 'utf8'
10-
script.async = true
14+
script.type = opts.type || 'text/javascript'
15+
script.charset = opts.charset || 'utf8';
16+
script.async = 'async' in opts ? !!opts.async : true
1117
script.src = src
1218

19+
if (opts.attrs) {
20+
setAttributes(script, opts.attrs)
21+
}
22+
23+
if (opts.text) {
24+
script.appendChild(document.createTextNode(opts.text))
25+
}
26+
1327
var onend = 'onload' in script ? stdOnEnd : ieOnEnd
1428
onend(script, cb)
1529

@@ -23,23 +37,29 @@ module.exports = function load (src, cb) {
2337
head.appendChild(script)
2438
}
2539

40+
function setAttributes(script, attrs) {
41+
for (var attr in attrs) {
42+
script.setAttribute(attr, attrs[attr]);
43+
}
44+
}
45+
2646
function stdOnEnd (script, cb) {
2747
script.onload = function () {
2848
this.onerror = this.onload = null
29-
cb()
49+
cb(null, script)
3050
}
3151
script.onerror = function () {
3252
// this.onload = null here is necessary
3353
// because even IE9 works not like others
3454
this.onerror = this.onload = null
35-
cb(new Error('Failed to load ' + this.src))
55+
cb(new Error('Failed to load ' + this.src), script)
3656
}
3757
}
3858

3959
function ieOnEnd (script, cb) {
4060
script.onreadystatechange = function () {
4161
if (this.readyState != 'complete' && this.readyState != 'loaded') return
4262
this.onreadystatechange = null
43-
cb(null, true) // there is no way to catch loading errors in IE8
63+
cb(null, script) // there is no way to catch loading errors in IE8
4464
}
4565
}

test/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,47 @@ test('success', function(done) {
1515
})
1616
});
1717

18+
test('opts.async', function(done) {
19+
load('test/hello.js', {async: false}, function(err, script) {
20+
assert.ifError(err);
21+
assert.equal(script.async, false);
22+
done();
23+
})
24+
});
25+
26+
test('opts.attrs', function(done) {
27+
load('test/hello.js', {attrs: {foo: 'boo'}}, function(err, script) {
28+
assert.ifError(err);
29+
assert.equal(script.getAttribute('foo'), 'boo');
30+
done();
31+
})
32+
});
33+
34+
test('opts.charset', function(done) {
35+
load('test/hello.js', {charset: 'iso-8859-1'}, function(err, script) {
36+
assert.ifError(err);
37+
assert.equal(script.charset, 'iso-8859-1');
38+
done();
39+
})
40+
});
41+
42+
test('opts.text', function(done) {
43+
load('test/hello.js', {text: 'foo'}, function(err, script) {
44+
assert.ifError(err);
45+
assert.equal(script.childNodes.length, 1);
46+
assert.equal(script.childNodes[0].nodeValue, 'foo');
47+
done();
48+
})
49+
});
50+
51+
test('opts.type', function(done) {
52+
load('test/hello.js', {type: 'text/ecmascript'}, function(err, script) {
53+
assert.ifError(err);
54+
assert.equal(script.type, 'text/ecmascript');
55+
done();
56+
})
57+
});
58+
1859
test('no exist', function(done) {
1960
load('unexistent.js', function (err, legacy) {
2061
if (!legacy) {

0 commit comments

Comments
 (0)