Skip to content

Commit 5795aba

Browse files
committed
Merge pull request #67 from e-sites/master
Added public urlArgs method (as suggested in #66)
2 parents d0186e3 + 6db705c commit 5795aba

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/script.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
, delay = {}
1616
, scripts = {}
1717
, scriptpath
18+
, urlArgs
1819

1920
function every(ar, fn) {
2021
for (var i = 0, j = ar.length; i < j; ++i) if (!fn(ar[i])) return f
@@ -47,7 +48,7 @@
4748
setTimeout(function () {
4849
each(paths, function loading(path, force) {
4950
if (path === null) return callback()
50-
path = !force && !/^https?:\/\//.test(path) && scriptpath ? scriptpath + path + '.js' : path
51+
path = !force && path.indexOf('.js') === -1 && !/^https?:\/\//.test(path) && scriptpath ? scriptpath + path + '.js' : path
5152
if (scripts[path]) {
5253
if (id) ids[id] = 1
5354
if (scripts[path] == 2) callback()
@@ -73,7 +74,7 @@
7374
fn()
7475
}
7576
el.async = 1
76-
el.src = path
77+
el.src = urlArgs ? path + (path.indexOf('?') === -1 ? '?' : '&') + urlArgs : path;
7778
head.insertBefore(el, head.lastChild)
7879
}
7980

@@ -89,6 +90,9 @@
8990
$script.path = function (p) {
9091
scriptpath = p
9192
}
93+
$script.urlArgs = function (str) {
94+
urlArgs = str;
95+
}
9296
$script.ready = function (deps, ready, req) {
9397
deps = deps[push] ? deps : [deps]
9498
var missing = [];

tests/tests.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ script('../node_modules/domready/ready.js', function () {
9090
ok(typeof morpheus !== 'undefined', 'loaded morpheus.js from http')
9191
})
9292
})
93+
94+
test('passing urlArgs', 2, function () {
95+
script.urlArgs('key=value')
96+
script(['../demos/js/foo.js'], function() {
97+
ok(true, 'foo has been loaded with given urlArgs')
98+
})
99+
script(['../demos/js/bar.js?foo=bar'], function() {
100+
ok(true, 'bar has been loaded and urlArgs have been added to existing query string')
101+
})
102+
})
93103

94104
test('syncronous ordered loading', 2, function () {
95105
script.order(['order-a', 'order-b', 'order-c'], 'ordered-id', function () {

0 commit comments

Comments
 (0)