scriptMgr.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ScriptLoader = function() {
  2. this.timeout = 10;
  3. this.scripts = [];
  4. this.disableCaching = true;//false
  5. this.loadMask = null;
  6. };
  7. ScriptLoader.prototype = {
  8. processSuccess : function(response) {
  9. if (typeof response.argument.callback == 'function') {
  10. response.argument.callback.call(response.argument.scope,
  11. response.responseText,response.argument.url,response.argument.index);
  12. }
  13. },
  14. load : function(url, callback) {
  15. var cfg, callerScope, index;
  16. if (typeof url == 'object') { // must be config object
  17. cfg = url;
  18. url = cfg.url;
  19. index=cfg.index;
  20. callback = callback || cfg.callback;
  21. callerScope = cfg.scope;
  22. if (typeof cfg.timeout != 'undefined') {
  23. this.timeout = cfg.timeout;
  24. }
  25. if (typeof cfg.disableCaching != 'undefined') {
  26. this.disableCaching = cfg.disableCaching;
  27. }
  28. }
  29. if (this.scripts[url]) {
  30. if (typeof callback == 'function') {
  31. callback.call(callerScope || window);
  32. }
  33. return null;
  34. }
  35. $.ajax({
  36. url: url,
  37. dataType: 'script',
  38. cache:!this.disableCaching,
  39. success: this.processSucess,
  40. argument : {
  41. 'url' : url,
  42. 'scope' : callerScope || window,
  43. 'callback' : callback,
  44. 'options' : cfg,
  45. 'index':index
  46. }
  47. });
  48. }
  49. };
  50. ScriptLoaderMgr=function() {
  51. //缓存加载内容
  52. this.mdCache=[];
  53. this.loader = new ScriptLoader();
  54. this.load = function(o) {
  55. this.loader.scope=o.scope;
  56. if(!o.scripts instanceof Array){
  57. o.scripts = [o.scripts];
  58. }
  59. //记数器
  60. o.lfiles=0;
  61. this.mdCache.length=0;
  62. var mdCache=this.mdCache;
  63. for(var i=0;i<o.scripts.length;i++){
  64. o.url = o.scripts[i];
  65. o.index=i;
  66. this.loader.load(o, function(rs,url,idx) {
  67. o.scope = this;
  68. mdCache[idx]={
  69. content:rs
  70. };
  71. o.lfiles++;
  72. if(o.lfiles>=o.scripts.length){
  73. //是否执行加载的js
  74. for(var j=0;j<mdCache.length;j++){
  75. try{
  76. window.execScript ? window.execScript(mdCache[j].content) : window.eval(mdCache[j].content);
  77. }catch(ex){
  78. }
  79. }
  80. if(o.callback!=null){
  81. o.callback.call(this);
  82. }
  83. }
  84. });
  85. }
  86. };
  87. };
  88. ScriptMgr = new ScriptLoaderMgr();