JAVASCRIPT   70
frequire js
Guest on 17th August 2022 12:25:18 AM


  1. function require(file, callback) {
  2.     if (file)
  3.     {
  4.         if (!constant.isDefined("API_URL")) {
  5.             constant.set('API_URL','http://funceme.br/produtos/js/');            
  6.         }
  7.         var name = $include.formatNameFile(file);
  8.        
  9.         var filejs = constant.get('API_URL')+file;
  10.        
  11.         var isInclude = $include.isScript(filejs);
  12.        
  13.         if (isInclude) {
  14.            
  15.             var newjs = document.createElement('script');
  16.             newjs.setAttribute('name', 'fapp');
  17.             //IE
  18.             newjs.onreadstatechange = function() {
  19.                
  20.                 if (newjs.readyState==='loaded'|| newjs.readyState==='complete') {
  21.                     newjs.onreadystatechange = null;
  22.                     if (typeof callback === "function") {
  23.                         callback();
  24.                     }
  25.                 }
  26.             }
  27.             //outros
  28.             newjs.onload = function() {
  29.                 if (typeof callback === "function") {
  30.                     callback();
  31.                 }  
  32.             }        
  33.             newjs.src = filejs;
  34.             document.body.appendChild(newjs);
  35.         }else if (typeof callback === "function") {
  36.             callback();
  37.         }
  38.        
  39.        
  40.     }      
  41. }
  42. var $include = {
  43.     formatNameFile : function(nome) {        
  44.         var nameArray = nome.split('/');        
  45.         nome = nameArray[1];        
  46.         return nome;
  47.     },
  48.     isScript : function(nome) {
  49.            
  50.         $('script[name=fapp]').each(function() {
  51.            
  52.             if ($(this).attr('src')==nome) {
  53.                    
  54.                 return false;
  55.             }
  56.         });
  57.            
  58.         return true;
  59.     }
  60. }
  61.  
  62. var constant = (function() {
  63.     var constants = {},
  64.     ownProp = Object.prototype.hasOwnProperty,
  65.     allowed = {
  66.         string : 1,
  67.         number : 1,
  68.         bolean : 1
  69.     },
  70.     prefix = (Math.random()+"_").slice(2);
  71.        
  72.     return {
  73.         set : function(name,value) {
  74.             if (this.isDefined(name)) {
  75.                 return false;
  76.             }
  77.             if (!ownProp.call(allowed,typeof value)) {
  78.                 return false;
  79.             }
  80.             constants[prefix+name] = value;
  81.                
  82.             return true;
  83.         },
  84.         isDefined : function(name) {
  85.             return ownProp.call(constants, prefix+name);
  86.         },
  87.         get : function(name) {
  88.             if (this.isDefined(name)) {
  89.                 return constants[prefix+name];
  90.             }
  91.             return null;
  92.         }
  93.     }
  94.    
  95. }());

Raw Paste

Login or Register to edit or fork this paste. It's free.