Skip to content

Commit 692a3f4

Browse files
committed
Add defer to options
1 parent 09f6c10 commit 692a3f4

2 files changed

Lines changed: 125 additions & 110 deletions

File tree

index.js

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
1+
module.exports = function load(src, opts, cb) {
2+
var head = document.head || document.getElementsByTagName('head')[0];
3+
var script = document.createElement('script');
14

2-
module.exports = function load (src, opts, cb) {
3-
var head = document.head || document.getElementsByTagName('head')[0]
4-
var script = document.createElement('script')
5+
if (typeof opts === 'function') {
6+
cb = opts;
7+
opts = {};
8+
}
59

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

11-
opts = opts || {}
12-
cb = cb || function() {}
13+
script.type = opts.type || 'text/javascript';
14+
script.charset = opts.charset || 'utf8';
15+
script.async = 'async' in opts ? !!opts.async : true;
16+
script.defer = 'defer' in opts ? !!opts.defer : true;
17+
script.src = src;
1318

14-
script.type = opts.type || 'text/javascript'
15-
script.charset = opts.charset || 'utf8';
16-
script.async = 'async' in opts ? !!opts.async : true
17-
script.src = src
19+
if (opts.attrs) {
20+
setAttributes(script, opts.attrs);
21+
}
1822

19-
if (opts.attrs) {
20-
setAttributes(script, opts.attrs)
21-
}
23+
if (opts.text) {
24+
script.text = '' + opts.text;
25+
}
2226

23-
if (opts.text) {
24-
script.text = '' + opts.text
25-
}
27+
var onend = 'onload' in script ? stdOnEnd : ieOnEnd;
28+
onend(script, cb);
2629

27-
var onend = 'onload' in script ? stdOnEnd : ieOnEnd
28-
onend(script, cb)
30+
// some good legacy browsers (firefox) fail the 'in' detection above
31+
// so as a fallback we always set onload
32+
// old IE will ignore this and new IE will set onload
33+
if (!script.onload) {
34+
stdOnEnd(script, cb);
35+
}
2936

30-
// some good legacy browsers (firefox) fail the 'in' detection above
31-
// so as a fallback we always set onload
32-
// old IE will ignore this and new IE will set onload
33-
if (!script.onload) {
34-
stdOnEnd(script, cb);
35-
}
36-
37-
head.appendChild(script)
38-
}
37+
head.appendChild(script);
38+
};
3939

4040
function setAttributes(script, attrs) {
41-
for (var attr in attrs) {
42-
script.setAttribute(attr, attrs[attr]);
43-
}
41+
for (var attr in attrs) {
42+
script.setAttribute(attr, attrs[attr]);
43+
}
4444
}
4545

46-
function stdOnEnd (script, cb) {
47-
script.onload = function () {
48-
this.onerror = this.onload = null
49-
cb(null, script)
50-
}
51-
script.onerror = function () {
52-
// this.onload = null here is necessary
53-
// because even IE9 works not like others
54-
this.onerror = this.onload = null
55-
cb(new Error('Failed to load ' + this.src), script)
56-
}
46+
function stdOnEnd(script, cb) {
47+
script.onload = function() {
48+
this.onerror = this.onload = null;
49+
cb(null, script);
50+
};
51+
script.onerror = function() {
52+
// this.onload = null here is necessary
53+
// because even IE9 works not like others
54+
this.onerror = this.onload = null;
55+
cb(new Error('Failed to load ' + this.src), script);
56+
};
5757
}
5858

59-
function ieOnEnd (script, cb) {
60-
script.onreadystatechange = function () {
61-
if (this.readyState != 'complete' && this.readyState != 'loaded') return
62-
this.onreadystatechange = null
63-
cb(null, script) // there is no way to catch loading errors in IE8
64-
}
59+
function ieOnEnd(script, cb) {
60+
script.onreadystatechange = function() {
61+
if (this.readyState != 'complete' && this.readyState != 'loaded') return;
62+
this.onreadystatechange = null;
63+
cb(null, script); // there is no way to catch loading errors in IE8
64+
};
6565
}

test/index.js

Lines changed: 75 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,105 @@
11
var assert = require('assert');
2-
var load = require('../')
2+
var load = require('../');
33

44
var last_msg = undefined;
55
log = function(msg) {
6-
last_msg = msg;
7-
}
6+
last_msg = msg;
7+
};
88

99
test('success', function(done) {
10-
load('test/hello.js', function (err) {
11-
assert.ifError(err);
12-
assert.equal(last_msg, 'Hello world');
13-
last_msg = undefined;
14-
done();
15-
})
10+
load('test/hello.js', function(err) {
11+
assert.ifError(err);
12+
assert.equal(last_msg, 'Hello world');
13+
last_msg = undefined;
14+
done();
15+
});
1616
});
1717

1818
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-
})
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.defer.false', function(done) {
27+
load('test/hello.js', { defer: false }, function(err, script) {
28+
assert.ifError(err);
29+
assert.equal(script.defer, false);
30+
done();
31+
});
32+
});
33+
34+
test('opts.defer.true', function(done) {
35+
load('test/hello.js', { defer: true }, function(err, script) {
36+
assert.ifError(err);
37+
assert.equal(script.defer, true);
38+
done();
39+
});
2440
});
2541

2642
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-
})
43+
load('test/hello.js', { attrs: { foo: 'boo' } }, function(err, script) {
44+
assert.ifError(err);
45+
assert.equal(script.getAttribute('foo'), 'boo');
46+
done();
47+
});
3248
});
3349

3450
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-
})
51+
load('test/hello.js', { charset: 'iso-8859-1' }, function(err, script) {
52+
assert.ifError(err);
53+
assert.equal(script.charset, 'iso-8859-1');
54+
done();
55+
});
4056
});
4157

4258
test('opts.text', function(done) {
43-
load('test/hello.js', {text: 'foo=5;'}, function(err, script) {
44-
assert.ifError(err);
45-
done();
46-
})
59+
load('test/hello.js', { text: 'foo=5;' }, function(err, script) {
60+
assert.ifError(err);
61+
done();
62+
});
4763
});
4864

4965
test('opts.type', function(done) {
50-
load('test/hello.js', {type: 'text/ecmascript'}, function(err, script) {
51-
assert.ifError(err);
52-
assert.equal(script.type, 'text/ecmascript');
53-
done();
54-
})
66+
load('test/hello.js', { type: 'text/ecmascript' }, function(err, script) {
67+
assert.ifError(err);
68+
assert.equal(script.type, 'text/ecmascript');
69+
done();
70+
});
5571
});
5672

5773
test('no exist', function(done) {
58-
load('unexistent.js', function (err, legacy) {
59-
if (!legacy) {
60-
assert.ok(err);
61-
}
74+
load('unexistent.js', function(err, legacy) {
75+
if (!legacy) {
76+
assert.ok(err);
77+
}
6278

63-
var tid = setTimeout(function() {
64-
done();
65-
}, 200);
79+
var tid = setTimeout(function() {
80+
done();
81+
}, 200);
6682

67-
// some browsers will also throw as well as report erro
68-
var old = window.onerror;
69-
window.onerror = function(msg, file, line) {
70-
if (msg !== 'Error loading script') {
71-
assert(false);
72-
}
73-
window.onerror = old;
74-
clearTimeout(tid);
75-
done();
76-
};
77-
})
83+
// some browsers will also throw as well as report erro
84+
var old = window.onerror;
85+
window.onerror = function(msg, file, line) {
86+
if (msg !== 'Error loading script') {
87+
assert(false);
88+
}
89+
window.onerror = old;
90+
clearTimeout(tid);
91+
done();
92+
};
93+
});
7894
});
7995

8096
test('throw', function(done) {
81-
var old = window.onerror;
82-
// silence the script error
83-
window.onerror = function() {};
84-
load('test/throw.js', function (err) {
85-
assert.ifError(err);
86-
window.onerror = old;
87-
done();
88-
})
97+
var old = window.onerror;
98+
// silence the script error
99+
window.onerror = function() {};
100+
load('test/throw.js', function(err) {
101+
assert.ifError(err);
102+
window.onerror = old;
103+
done();
104+
});
89105
});
90-

0 commit comments

Comments
 (0)