script.js 8.6 KB

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