1 define("dojo/request/script", [
11 '../_base/window'/*=====,
13 '../_base/declare' =====*/
14 ], function(module, watch, util, array, lang, on, dom, domConstruct, has, win/*=====, request, declare =====*/){
15 has.add('script-readystatechange', function(global, document){
16 var script = document.createElement('script');
17 return typeof script['onreadystatechange'] !== 'undefined' &&
18 (typeof global['opera'] === 'undefined' || global['opera'].toString() !== '[object Opera]');
21 var mid = module.id.replace(/[\/\.\-]/g, '_'),
23 loadEvent = has('script-readystatechange') ? 'readystatechange' : 'load',
24 readyRegExp = /complete|loaded/,
25 callbacks = this[mid + '_callbacks'] = {},
28 function attach(id, url, frameDoc){
29 var doc = (frameDoc || win.doc),
30 element = doc.createElement('script');
32 element.type = 'text/javascript';
36 element.charset = 'utf-8';
38 return doc.getElementsByTagName('head')[0].appendChild(element);
41 function remove(id, frameDoc, cleanup){
42 domConstruct.destroy(dom.byId(id, frameDoc));
46 // set callback to a function that deletes itself so requests that
47 // are in-flight don't error out when returning and also
48 // clean up after themselves
49 callbacks[id] = function(){
58 function _addDeadScript(dfd){
59 // Be sure to check ioArgs because it can dynamically change in the dojox/io plugins.
60 // See http://bugs.dojotoolkit.org/ticket/15890.
61 var options = dfd.response.options,
62 frameDoc = options.ioArgs ? options.ioArgs.frameDoc : options.frameDoc;
64 deadScripts.push({ id: dfd.id, frameDoc: frameDoc });
67 options.ioArgs.frameDoc = null;
69 options.frameDoc = null;
72 function canceler(dfd, response){
74 //For timeouts and cancels, remove the script element immediately to
75 //avoid a response from it coming back later and causing trouble.
76 script._remove(dfd.id, response.options.frameDoc, true);
79 function isValid(response){
80 //Do script cleanup here. We wait for one inflight pass
81 //to make sure we don't get any weird things by trying to remove a script
82 //tag that is part of the call chain (IE 6 has been known to
83 //crash in that case).
84 if(deadScripts && deadScripts.length){
85 array.forEach(deadScripts, function(_script){
86 script._remove(_script.id, _script.frameDoc);
87 _script.frameDoc = null;
92 return response.options.jsonp ? !response.data : true;
94 function isReadyScript(response){
95 return !!this.scriptLoaded;
97 function isReadyCheckString(response){
98 var checkString = response.options.checkString;
100 return checkString && eval('typeof(' + checkString + ') !== "undefined"');
102 function handleResponse(response, error){
104 _addDeadScript(this);
109 this.resolve(response);
113 function script(url, options, returnDeferred){
114 var response = util.parseArgs(url, util.deepCopy({}, options));
116 options = response.options;
118 var dfd = util.deferred(
122 options.jsonp ? null : (options.checkString ? isReadyCheckString : isReadyScript),
127 id: mid + (counter++),
132 var queryParameter = new RegExp('[?&]' + options.jsonp + '=');
133 if(!queryParameter.test(url)){
134 url += queryParameter +
135 (options.frameDoc ? 'parent.' : '') +
136 mid + '_callbacks.' + dfd.id;
139 dfd.canDelete = true;
140 callbacks[dfd.id] = function(json){
141 response.data = json;
142 dfd.handleResponse(response);
147 util.notify.emit('send', response, dfd.promise.cancel);
150 if(!options.canAttach || options.canAttach(dfd)){
151 var node = script._attach(dfd.id, url, options.frameDoc);
153 if(!options.jsonp && !options.checkString){
154 var handle = on(node, loadEvent, function(evt){
155 if(evt.type === 'load' || readyRegExp.test(node.readyState)){
157 dfd.scriptLoaded = evt;
165 return returnDeferred ? dfd : dfd.promise;
169 script = function(url, options){
171 // Sends a request using a script element with the given URL and options.
174 // options: dojo/request/script.__Options?
175 // Options for the request.
176 // returns: dojo/request.__Promise
178 script.__BaseOptions = declare(request.__BaseOptions, {
180 // The URL parameter name that indicates the JSONP callback string.
181 // For instance, when using Yahoo JSONP calls it is normally,
182 // jsonp: "callback". For AOL JSONP calls it is normally
184 // checkString: String?
185 // A string of JavaScript that when evaluated like so:
186 // "typeof(" + checkString + ") != 'undefined'"
187 // being true means that the script fetched has been loaded.
188 // Do not use this if doing a JSONP type of call (use `jsonp` instead).
189 // frameDoc: Document?
190 // The Document object of a child iframe. If this is passed in, the script
191 // will be attached to that document. This can be helpful in some comet long-polling
192 // scenarios with Firefox and Opera.
194 script.__MethodOptions = declare(null, {
196 // This option is ignored. All requests using this transport are
199 script.__Options = declare([script.__BaseOptions, script.__MethodOptions]);
201 script.get = function(url, options){
203 // Send an HTTP GET request using a script element with the given URL and options.
206 // options: dojo/request/script.__BaseOptions?
207 // Options for the request.
208 // returns: dojo/request.__Promise
212 // TODO: Remove in 2.0
213 script._attach = attach;
214 script._remove = remove;
215 script._callbacksProperty = mid + '_callbacks';