Changeset 1230


Ignore:
Timestamp:
11/15/09 02:11:02 (7 years ago)
Author:
ray
Message:

avoid unecessary pollution of global namespace

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/XinhaLoader.js

    r1157 r1230  
    22 
    33// Auto detect _editor_url if it's not set. 
    4 if (!window._editor_url) { 
    5   // Because of the way the DOM is loaded, this is guaranteed to always pull our script tag. 
    6   var scripts = document.getElementsByTagName('script'); 
    7   var this_script = scripts[scripts.length-1]; 
    8  
    9   // We'll allow two ways to specify arguments.  We'll accept them in the 
    10   // argument of the script, or we'll accept them embedded into our script tag. 
    11   var args = this_script.src.split('?'); 
    12   args = args.length == 2 ? args[1].split('&') : ''; 
    13   for (var index=0; index<args.length; ++index) { 
    14     var arg = args[index].split('='); 
    15     if (arg.length == 2) { 
    16         switch (arg[0]) { 
    17             case 'lang': 
    18             case 'icons': 
    19             case 'skin': 
    20             case 'url': 
    21               window['_editor_'+arg[0]] = arg[1]; 
    22               break; 
     4if (!window._editor_url)  
     5{ 
     6  (function() // wrap this in an ad-hoc function to avoid unecessary pollution of global namespace 
     7  { 
     8    // Because of the way the DOM is loaded, this is guaranteed to always pull our script tag. 
     9    var scripts = document.getElementsByTagName('script'); 
     10    var this_script = scripts[scripts.length - 1]; 
     11   
     12    // We'll allow two ways to specify arguments.  We'll accept them in the 
     13    // argument of the script, or we'll accept them embedded into our script tag. 
     14    var args = this_script.src.split('?'); 
     15    args = args.length == 2 ? args[1].split('&') : ''; 
     16    for (var index = 0; index < args.length; ++index)  
     17    { 
     18      var arg = args[index].split('='); 
     19      if (arg.length == 2)  
     20      { 
     21        switch (arg[0]) 
     22        { 
     23          case 'lang': 
     24          case 'icons': 
     25          case 'skin': 
     26          case 'url': 
     27            window['_editor_' + arg[0]] = arg[1]; 
     28            break; 
    2329        } 
    24     } 
    25   } 
    26  
    27   // We can grab the script innerHTML and execute that to cut down on script 
    28   // tags.  Thanks John Resig! 
    29   // http://ejohn.org/blog/degrading-script-tags/ 
    30   if (this_script.innerHTML.replace(/\s+/,'')) { 
     30      } 
     31    } 
     32     
     33    // We can grab the script innerHTML and execute that to cut down on script 
     34    // tags.  Thanks John Resig! 
     35    // http://ejohn.org/blog/degrading-script-tags/ 
     36    if (this_script.innerHTML.replace(/\s+/, ''))  
     37    { 
    3138      eval(this_script.innerHTML); 
    32   } 
    33  
    34   // Default values 
    35   _editor_lang = window._editor_lang || 'de'; 
    36  
    37   // Chop off any query string.  Chop the filename off of the URL. 
    38   _editor_url = window._editor_url || this_script.src.split('?')[0].split('/').slice(0,-1).join('/'); 
    39 } 
    40  
     39    } 
     40     
     41    // Default values 
     42    _editor_lang = window._editor_lang || 'en'; 
     43     
     44    // Chop off any query string.  Chop the filename off of the URL. 
     45    _editor_url = window._editor_url || this_script.src.split('?')[0].split('/').slice(0, -1).join('/'); 
     46 
     47  })() 
     48} 
    4149_editor_url = _editor_url.replace(/\x2f*$/, '/'); 
    4250 
Note: See TracChangeset for help on using the changeset viewer.