-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
85 lines (75 loc) · 2.75 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
define(["dojo"], function(dojo){
// one-time lookups / vars:
var d = dojo, h = d.doc.getElementsByTagName("head")[0],
re = /complete|loaded/,
cbtest = /(\w+)=\?/,
count = 0
;
d.addScript = function(src, callback, preserve){
// summary: A simplified version of `dojo.io.script.get`. Handle's cross-domain loading of
// resources that may not otherwise be provided as JSONP and meant entirely to be
// consumed upon loading.
//
// src: String
// The URL of a script to inject into this page. Can be local or cross-domain.
//
// callback: Function?
// Optional callback function to execute on completion of the script. No failure
// can be trapped. A timeout could easily be incorporated. The event which triggered
// the success is passed to the callback.
//
// preserve: Boolean?
// If true, the newly created script tag will be left in the DOM after having completed.
// If false (default) the script will be removed from the DOM.
//
// example:
// | dojo.addScript("http://example.com/js.js", function(e){ /* js.js is ready */ });
//
// example:
// | dojo.addScript("foo.js", dojo.hitch(this, "handler"));
//
// example:
// | dojo.addScript(dojo.moduleUrl("dijit", "Dialog.js"), function(e){
// | // only Dialog.js onload, not it's require() calls. use `dojo.require` + `dojo.addOnLoad`
// | });
//
if(cbtest.test(src)){
// our script was passed some form of callback lamda. fix it.
d._getJsonp.apply(this, arguments);
}else{
// actual handling of the script addition and onload
var s = d.create("script", { src: src, async:true }, h),
c = d.connect(s, s.readyState ? "onreadystatechange" : "load", function(e){
if(e.type == "load" || re.test(s.readyState)){
d.disconnect(c);
callback && callback.call(s, e);
!preserve && h.removeChild(s);
}
})
;
}
}
d._getJsonp = function(src, callback, preserve){
// summary:
// Small branch of `dojo.addScript` for the x-domain JSONP case. If the url passed to
// `dojo.addScript` contains a literal "something=?", the ? is replaced
// with a generated callback function, passed as `callback`, and triggered.
//
// example:
// | dojo._getJsonp("http://example.com/url.json?callback=?", function(data){
// | // data is whatever the response data was. this is async.
// | });
// setup the callback information:
var id = "cb" + (++count);
d.addScript[id] = callback;
// adjust the src to contain a valid callback param
src = src.replace(cbtest, function(_, b){
return b + "=" + d._scopeName + ".addScript." + id;
});
// add the script, and delete the callback function onload
d.addScript(src, function(){
delete d.addScript[id];
}, preserve);
}
return d.addScript;
});