JAVASCRIPT 8
Async.js Guest on 6th April 2021 03:08:17 AM
  1. /** @license
  2.  * RequireJS plugin for async dependency load like JSONP and Google Maps
  3.  * Author: Miller Medeiros
  4.  * Version: 0.1.2 (2014/02/24)
  5.  * Released under the MIT license
  6.  */
  7. define(function(){
  8.  
  9.     var DEFAULT_PARAM_NAME = 'callback',
  10.         _uid = 0;
  11.  
  12.     function injectScript(src){
  13.         var s, t;
  14.         s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = src;
  15.         t = document.getElementsByTagName('script')[0]; t.parentNode.insertBefore(s,t);
  16.     }
  17.  
  18.     function formatUrl(name, id){
  19.         var paramRegex = /!(.+)/,
  20.             url = name.replace(paramRegex, ''),
  21.             param = (paramRegex.test(name))? name.replace(/.+!/, '') : DEFAULT_PARAM_NAME;
  22.         url += (url.indexOf('?') < 0)? '?' : '&';
  23.         return url + param +'='+ id;
  24.     }
  25.  
  26.     function uid() {
  27.         _uid += 1;
  28.         return '__async_req_'+ _uid +'__';
  29.     }
  30.  
  31.     return{
  32.         load : function(name, req, onLoad, config){
  33.             if(config.isBuild){
  34.                 onLoad(null); //avoid errors on the optimizer
  35.             }else{
  36.                 var id = uid();
  37.                 //create a global variable that stores onLoad so callback
  38.                 //function can define new module after async load
  39.                 window[id] = onLoad;
  40.                 injectScript(formatUrl(req.toUrl(name), id));
  41.             }
  42.         }
  43.     };
  44. });

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

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