script.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. define("dojo/io/script", ["../main"], function(dojo) {
  2. // module:
  3. // dojo/io/script
  4. // summary:
  5. // TODOC
  6. dojo.getObject("io", true, dojo);
  7. /*=====
  8. dojo.declare("dojo.io.script.__ioArgs", dojo.__IoArgs, {
  9. constructor: function(){
  10. // summary:
  11. // All the properties described in the dojo.__ioArgs type, apply to this
  12. // type as well, EXCEPT "handleAs". It is not applicable to
  13. // dojo.io.script.get() calls, since it is implied by the usage of
  14. // "jsonp" (response will be a JSONP call returning JSON)
  15. // or the response is pure JavaScript defined in
  16. // the body of the script that was attached.
  17. // callbackParamName: String
  18. // Deprecated as of Dojo 1.4 in favor of "jsonp", but still supported for
  19. // legacy code. See notes for jsonp property.
  20. // jsonp: String
  21. // The URL parameter name that indicates the JSONP callback string.
  22. // For instance, when using Yahoo JSONP calls it is normally,
  23. // jsonp: "callback". For AOL JSONP calls it is normally
  24. // jsonp: "c".
  25. // checkString: String
  26. // A string of JavaScript that when evaluated like so:
  27. // "typeof(" + checkString + ") != 'undefined'"
  28. // being true means that the script fetched has been loaded.
  29. // Do not use this if doing a JSONP type of call (use callbackParamName instead).
  30. // frameDoc: Document
  31. // The Document object for a child iframe. If this is passed in, the script
  32. // will be attached to that document. This can be helpful in some comet long-polling
  33. // scenarios with Firefox and Opera.
  34. this.callbackParamName = callbackParamName;
  35. this.jsonp = jsonp;
  36. this.checkString = checkString;
  37. this.frameDoc = frameDoc;
  38. }
  39. });
  40. =====*/
  41. var loadEvent = dojo.isIE ? "onreadystatechange" : "load",
  42. readyRegExp = /complete|loaded/;
  43. dojo.io.script = {
  44. get: function(/*dojo.io.script.__ioArgs*/args){
  45. // summary:
  46. // sends a get request using a dynamically created script tag.
  47. var dfd = this._makeScriptDeferred(args);
  48. var ioArgs = dfd.ioArgs;
  49. dojo._ioAddQueryToUrl(ioArgs);
  50. dojo._ioNotifyStart(dfd);
  51. if(this._canAttach(ioArgs)){
  52. var node = this.attach(ioArgs.id, ioArgs.url, args.frameDoc);
  53. //If not a jsonp callback or a polling checkString case, bind
  54. //to load event on the script tag.
  55. if(!ioArgs.jsonp && !ioArgs.args.checkString){
  56. var handle = dojo.connect(node, loadEvent, function(evt){
  57. if(evt.type == "load" || readyRegExp.test(node.readyState)){
  58. dojo.disconnect(handle);
  59. ioArgs.scriptLoaded = evt;
  60. }
  61. });
  62. }
  63. }
  64. dojo._ioWatch(dfd, this._validCheck, this._ioCheck, this._resHandle);
  65. return dfd;
  66. },
  67. attach: function(/*String*/id, /*String*/url, /*Document?*/frameDocument){
  68. // summary:
  69. // creates a new <script> tag pointing to the specified URL and
  70. // adds it to the document.
  71. // description:
  72. // Attaches the script element to the DOM. Use this method if you
  73. // just want to attach a script to the DOM and do not care when or
  74. // if it loads.
  75. var doc = (frameDocument || dojo.doc);
  76. var element = doc.createElement("script");
  77. element.type = "text/javascript";
  78. element.src = url;
  79. element.id = id;
  80. element.async = true;
  81. element.charset = "utf-8";
  82. return doc.getElementsByTagName("head")[0].appendChild(element);
  83. },
  84. remove: function(/*String*/id, /*Document?*/frameDocument){
  85. //summary: removes the script element with the given id, from the given frameDocument.
  86. //If no frameDocument is passed, the current document is used.
  87. dojo.destroy(dojo.byId(id, frameDocument));
  88. //Remove the jsonp callback on dojo.io.script, if it exists.
  89. if(this["jsonp_" + id]){
  90. delete this["jsonp_" + id];
  91. }
  92. },
  93. _makeScriptDeferred: function(/*Object*/args){
  94. //summary:
  95. // sets up a Deferred object for an IO request.
  96. var dfd = dojo._ioSetArgs(args, this._deferredCancel, this._deferredOk, this._deferredError);
  97. var ioArgs = dfd.ioArgs;
  98. ioArgs.id = dojo._scopeName + "IoScript" + (this._counter++);
  99. ioArgs.canDelete = false;
  100. //Special setup for jsonp case
  101. ioArgs.jsonp = args.callbackParamName || args.jsonp;
  102. if(ioArgs.jsonp){
  103. //Add the jsonp parameter.
  104. ioArgs.query = ioArgs.query || "";
  105. if(ioArgs.query.length > 0){
  106. ioArgs.query += "&";
  107. }
  108. ioArgs.query += ioArgs.jsonp
  109. + "="
  110. + (args.frameDoc ? "parent." : "")
  111. + dojo._scopeName + ".io.script.jsonp_" + ioArgs.id + "._jsonpCallback";
  112. ioArgs.frameDoc = args.frameDoc;
  113. //Setup the Deferred to have the jsonp callback.
  114. ioArgs.canDelete = true;
  115. dfd._jsonpCallback = this._jsonpCallback;
  116. this["jsonp_" + ioArgs.id] = dfd;
  117. }
  118. return dfd; // dojo.Deferred
  119. },
  120. _deferredCancel: function(/*Deferred*/dfd){
  121. //summary: canceller function for dojo._ioSetArgs call.
  122. //DO NOT use "this" and expect it to be dojo.io.script.
  123. dfd.canceled = true;
  124. if(dfd.ioArgs.canDelete){
  125. dojo.io.script._addDeadScript(dfd.ioArgs);
  126. }
  127. },
  128. _deferredOk: function(/*Deferred*/dfd){
  129. //summary: okHandler function for dojo._ioSetArgs call.
  130. //DO NOT use "this" and expect it to be dojo.io.script.
  131. var ioArgs = dfd.ioArgs;
  132. //Add script to list of things that can be removed.
  133. if(ioArgs.canDelete){
  134. dojo.io.script._addDeadScript(ioArgs);
  135. }
  136. //Favor JSONP responses, script load events then lastly ioArgs.
  137. //The ioArgs are goofy, but cannot return the dfd since that stops
  138. //the callback chain in Deferred. The return value is not that important
  139. //in that case, probably a checkString case.
  140. return ioArgs.json || ioArgs.scriptLoaded || ioArgs;
  141. },
  142. _deferredError: function(/*Error*/error, /*Deferred*/dfd){
  143. //summary: errHandler function for dojo._ioSetArgs call.
  144. if(dfd.ioArgs.canDelete){
  145. //DO NOT use "this" and expect it to be dojo.io.script.
  146. if(error.dojoType == "timeout"){
  147. //For timeouts, remove the script element immediately to
  148. //avoid a response from it coming back later and causing trouble.
  149. dojo.io.script.remove(dfd.ioArgs.id, dfd.ioArgs.frameDoc);
  150. }else{
  151. dojo.io.script._addDeadScript(dfd.ioArgs);
  152. }
  153. }
  154. console.log("dojo.io.script error", error);
  155. return error;
  156. },
  157. _deadScripts: [],
  158. _counter: 1,
  159. _addDeadScript: function(/*Object*/ioArgs){
  160. //summary: sets up an entry in the deadScripts array.
  161. dojo.io.script._deadScripts.push({id: ioArgs.id, frameDoc: ioArgs.frameDoc});
  162. //Being extra paranoid about leaks:
  163. ioArgs.frameDoc = null;
  164. },
  165. _validCheck: function(/*Deferred*/dfd){
  166. //summary: inflight check function to see if dfd is still valid.
  167. //Do script cleanup here. We wait for one inflight pass
  168. //to make sure we don't get any weird things by trying to remove a script
  169. //tag that is part of the call chain (IE 6 has been known to
  170. //crash in that case).
  171. var _self = dojo.io.script;
  172. var deadScripts = _self._deadScripts;
  173. if(deadScripts && deadScripts.length > 0){
  174. for(var i = 0; i < deadScripts.length; i++){
  175. //Remove the script tag
  176. _self.remove(deadScripts[i].id, deadScripts[i].frameDoc);
  177. deadScripts[i].frameDoc = null;
  178. }
  179. dojo.io.script._deadScripts = [];
  180. }
  181. return true;
  182. },
  183. _ioCheck: function(/*Deferred*/dfd){
  184. //summary: inflight check function to see if IO finished.
  185. var ioArgs = dfd.ioArgs;
  186. //Check for finished jsonp
  187. if(ioArgs.json || (ioArgs.scriptLoaded && !ioArgs.args.checkString)){
  188. return true;
  189. }
  190. //Check for finished "checkString" case.
  191. var checkString = ioArgs.args.checkString;
  192. return checkString && eval("typeof(" + checkString + ") != 'undefined'");
  193. },
  194. _resHandle: function(/*Deferred*/dfd){
  195. //summary: inflight function to handle a completed response.
  196. if(dojo.io.script._ioCheck(dfd)){
  197. dfd.callback(dfd);
  198. }else{
  199. //This path should never happen since the only way we can get
  200. //to _resHandle is if _ioCheck is true.
  201. dfd.errback(new Error("inconceivable dojo.io.script._resHandle error"));
  202. }
  203. },
  204. _canAttach: function(/*Object*/ioArgs){
  205. //summary: A method that can be overridden by other modules
  206. //to control when the script attachment occurs.
  207. return true;
  208. },
  209. _jsonpCallback: function(/*JSON Object*/json){
  210. //summary:
  211. // generic handler for jsonp callback. A pointer to this function
  212. // is used for all jsonp callbacks. NOTE: the "this" in this
  213. // function will be the Deferred object that represents the script
  214. // request.
  215. this.ioArgs.json = json;
  216. }
  217. };
  218. return dojo.io.script;
  219. });