dom_request_script.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. -----------------------------------------------------------
  3. dom.loadScript.js : 0.1.4 : 2014/02/12 : http://mudcu.be
  4. -----------------------------------------------------------
  5. Copyright 2011-2014 Mudcube. All rights reserved.
  6. -----------------------------------------------------------
  7. /// No verification
  8. dom.loadScript.add("../js/jszip/jszip.js");
  9. /// Strict loading order and verification.
  10. dom.loadScript.add({
  11. strictOrder: true,
  12. urls: [
  13. {
  14. url: "../js/jszip/jszip.js",
  15. verify: "JSZip",
  16. onsuccess: function() {
  17. console.log(1)
  18. }
  19. },
  20. {
  21. url: "../inc/downloadify/js/swfobject.js",
  22. verify: "swfobject",
  23. onsuccess: function() {
  24. console.log(2)
  25. }
  26. }
  27. ],
  28. onsuccess: function() {
  29. console.log(3)
  30. }
  31. });
  32. /// Just verification.
  33. dom.loadScript.add({
  34. url: "../js/jszip/jszip.js",
  35. verify: "JSZip",
  36. onsuccess: function() {
  37. console.log(1)
  38. }
  39. });
  40. */
  41. if (typeof(dom) === "undefined") var dom = {};
  42. (function() { "use strict";
  43. dom.loadScript = function() {
  44. this.loaded = {};
  45. this.loading = {};
  46. return this;
  47. };
  48. dom.loadScript.prototype.add = function(config) {
  49. var that = this;
  50. if (typeof(config) === "string") {
  51. config = { url: config };
  52. }
  53. var urls = config.urls;
  54. if (typeof(urls) === "undefined") {
  55. urls = [{
  56. url: config.url,
  57. verify: config.verify
  58. }];
  59. }
  60. /// adding the elements to the head
  61. var doc = document.getElementsByTagName("head")[0];
  62. ///
  63. var testElement = function(element, test) {
  64. if (that.loaded[element.url]) return;
  65. if (test && globalExists(test) === false) return;
  66. that.loaded[element.url] = true;
  67. //
  68. if (that.loading[element.url]) that.loading[element.url]();
  69. delete that.loading[element.url];
  70. //
  71. if (element.onsuccess) element.onsuccess();
  72. if (typeof(getNext) !== "undefined") getNext();
  73. };
  74. ///
  75. var hasError = false;
  76. var batchTest = [];
  77. var addElement = function(element) {
  78. if (typeof(element) === "string") {
  79. element = {
  80. url: element,
  81. verify: config.verify
  82. };
  83. }
  84. if (/([\w\d.\[\]\'\"])$/.test(element.verify)) { // check whether its a variable reference
  85. var verify = element.test = element.verify;
  86. if (typeof(verify) === "object") {
  87. for (var n = 0; n < verify.length; n ++) {
  88. batchTest.push(verify[n]);
  89. }
  90. } else {
  91. batchTest.push(verify);
  92. }
  93. }
  94. if (that.loaded[element.url]) return;
  95. var script = document.createElement("script");
  96. script.onreadystatechange = function() {
  97. if (this.readyState !== "loaded" && this.readyState !== "complete") return;
  98. testElement(element);
  99. };
  100. script.onload = function() {
  101. testElement(element);
  102. };
  103. script.onerror = function() {
  104. hasError = true;
  105. delete that.loading[element.url];
  106. if (typeof(element.test) === "object") {
  107. for (var key in element.test) {
  108. removeTest(element.test[key]);
  109. }
  110. } else {
  111. removeTest(element.test);
  112. }
  113. };
  114. script.setAttribute("type", "text/javascript");
  115. script.setAttribute("src", element.url);
  116. doc.appendChild(script);
  117. that.loading[element.url] = function() {};
  118. };
  119. /// checking to see whether everything loaded properly
  120. var removeTest = function(test) {
  121. var ret = [];
  122. for (var n = 0; n < batchTest.length; n ++) {
  123. if (batchTest[n] === test) continue;
  124. ret.push(batchTest[n]);
  125. }
  126. batchTest = ret;
  127. };
  128. var onLoad = function(element) {
  129. if (element) {
  130. testElement(element, element.test);
  131. } else {
  132. for (var n = 0; n < urls.length; n ++) {
  133. testElement(urls[n], urls[n].test);
  134. }
  135. }
  136. var istrue = true;
  137. for (var n = 0; n < batchTest.length; n ++) {
  138. if (globalExists(batchTest[n]) === false) {
  139. istrue = false;
  140. }
  141. }
  142. if (!config.strictOrder && istrue) { // finished loading all the requested scripts
  143. if (hasError) {
  144. if (config.error) {
  145. config.error();
  146. }
  147. } else if (config.onsuccess) {
  148. config.onsuccess();
  149. }
  150. } else { // keep calling back the function
  151. setTimeout(function() { //- should get slower over time?
  152. onLoad(element);
  153. }, 10);
  154. }
  155. };
  156. /// loading methods; strict ordering or loose ordering
  157. if (config.strictOrder) {
  158. var ID = -1;
  159. var getNext = function() {
  160. ID ++;
  161. if (!urls[ID]) { // all elements are loaded
  162. if (hasError) {
  163. if (config.error) {
  164. config.error();
  165. }
  166. } else if (config.onsuccess) {
  167. config.onsuccess();
  168. }
  169. } else { // loading new script
  170. var element = urls[ID];
  171. var url = element.url;
  172. if (that.loading[url]) { // already loading from another call (attach to event)
  173. that.loading[url] = function() {
  174. if (element.onsuccess) element.onsuccess();
  175. getNext();
  176. }
  177. } else if (!that.loaded[url]) { // create script element
  178. addElement(element);
  179. onLoad(element);
  180. } else { // it's already been successfully loaded
  181. getNext();
  182. }
  183. }
  184. };
  185. getNext();
  186. } else { // loose ordering
  187. for (var ID = 0; ID < urls.length; ID ++) {
  188. addElement(urls[ID]);
  189. onLoad(urls[ID]);
  190. }
  191. }
  192. };
  193. dom.loadScript = new dom.loadScript();
  194. var globalExists = function(path, root) {
  195. try {
  196. path = path.split('"').join('').split("'").join('').split(']').join('').split('[').join('.');
  197. var parts = path.split(".");
  198. var length = parts.length;
  199. var object = root || window;
  200. for (var n = 0; n < length; n ++) {
  201. var key = parts[n];
  202. if (object[key] == null) {
  203. return false;
  204. } else { //
  205. object = object[key];
  206. }
  207. }
  208. return true;
  209. } catch(e) {
  210. return false;
  211. }
  212. };
  213. })();
  214. /// For NodeJS
  215. if (typeof (module) !== "undefined" && module.exports) {
  216. module.exports = dom.loadScript;
  217. }