Advertisement
Gerst20051

JavaScript Namespacing

Nov 20th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // STAGE I
  2. var MYGLOBAL = window.MYGLOBAL || {};
  3. MYGLOBAL.myProgram = function () {
  4.     return {
  5.         init: function () {
  6.             alert('Hello, world!');
  7.         }
  8.     };
  9. }();
  10.  
  11. window.onload = function () {
  12.     MYGLOBAL.myProgram.init();
  13. };
  14.  
  15.  
  16. // STAGE II
  17.  
  18. var trueName = '';
  19. for (var i = 0; i < 16; i++) {
  20.     trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
  21. }
  22. window[trueName] = {};
  23. window[trueName].myProgram = function () {
  24.     return {
  25.         init: function () {
  26.             alert('Hello, ' + trueName + '!');
  27.         }
  28.     };
  29. }();
  30. window.onload = function () {
  31.     window[trueName].myProgram.init();
  32. };
  33.  
  34.  
  35. // STAGE III
  36.  
  37. (function () {
  38.     var trueName = '';
  39.     for (var i = 0; i < 16; i++) {
  40.         trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
  41.     }
  42.     window[trueName] = {};
  43.     var $ = window[trueName];
  44.     $.f = function () {
  45.         return {
  46.             init: function () {
  47.                 alert('Hello again, ' + trueName + '!');
  48.             }
  49.         };
  50.     }();
  51.     window.onload = function () {
  52.         $.f.init();
  53.     };
  54. })();
  55.  
  56.  
  57. // STAGE IV
  58.  
  59. (function () {
  60.     var trueName = '';
  61.     for (var i = 0; i < 16; i++) {
  62.         trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
  63.     }
  64.     window[trueName] = {};
  65.     var $ = window[trueName];
  66.     $.f = function () {
  67.         return {
  68.             init: function (target) {
  69.                 var theScripts = document.getElementsByTagName('SCRIPT');
  70.                 for (var i = 0; i < theScripts.length; i++) {
  71.                     if (theScripts[i].src.match(target)) {
  72.                         $.w = document.createElement('DIV');
  73.                         $.w.innerHTML = 'Hello, world.  My name is ' + trueName + '.';
  74.                         theScripts[i].parentNode.insertBefore($.w, theScripts[i]);
  75.                         theScripts[i].parentNode.removeChild(theScripts[i]);
  76.                         break;
  77.                     }
  78.                 }
  79.             }
  80.         };
  81.     }();
  82.     var thisScript = /behavior.js/;
  83.     var thisScriptProduction = /^https?:\/\/[^\/]*yourdomain.com\/yourpath\/behavior.js$/;
  84.     window.onload = function () {
  85.         $.f.init(thisScript);
  86.     };
  87. })();
  88.  
  89.  
  90. // STAGE V
  91.  
  92. (function () {
  93.     var trueName = '';
  94.     for (var i = 0; i < 16; i++) {
  95.         trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
  96.     }
  97.     window[trueName] = {};
  98.     var $ = window[trueName];
  99.     $.f = function () {
  100.         return {
  101.             init: function (target) {
  102.                 var theScripts = document.getElementsByTagName('SCRIPT');
  103.                 for (var i = 0; i < theScripts.length; i++) {
  104.                     if (theScripts[i].src.match(target)) {
  105.                         $.w = document.createElement('DIV');
  106.                         $.w.innerHTML = 'Hello, world.  My name is ' + trueName + '.';
  107.                         theScripts[i].parentNode.insertBefore($.w, theScripts[i]);
  108.                         theScripts[i].parentNode.removeChild(theScripts[i]);
  109.                         break;
  110.                     }
  111.                 }
  112.             }
  113.         };
  114.     }();
  115.     var thisScript = /behavior.js/;
  116.     var thisScriptProduction = /^https?:\/\/[^\/]*yourdomain.com\/yourpath\/behavior.js$/;
  117.     if (typeof window.addEventListener !== 'undefined') {
  118.         window.addEventListener('load', function () {
  119.             $.f.init(thisScript);
  120.         }, false);
  121.     } else if (typeof window.attachEvent !== 'undefined') {
  122.         window.attachEvent('onload', function () {
  123.             $.f.init(thisScript);
  124.         });
  125.     }
  126. })();
  127.  
  128.  
  129. // STAGE VI
  130.  
  131. (function () {
  132.     var trueName = '';
  133.     for (var i = 0; i < 16; i++) {
  134.         trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
  135.     }
  136.     window[trueName] = {};
  137.     var $ = window[trueName];
  138.     $.f = function () {
  139.         return {
  140.             init: function (target) {
  141.                 var theScripts = document.getElementsByTagName('SCRIPT');
  142.                 for (var i = 0; i < theScripts.length; i++) {
  143.                     if (theScripts[i].src.match(target)) {
  144.                         $.w = document.createElement('DIV');
  145.                         $.w.innerHTML = 'Hello, world.  My name is ' + trueName + '.';
  146.                         $.a = {};
  147.                         if (theScripts[i].innerHTML) {
  148.                             $.a = $.f.parseJson(theScripts[i].innerHTML);
  149.                         }
  150.                         if ($.a.err) {
  151.                             alert('bad json!');
  152.                         }
  153.                         if ($.a.color) {
  154.                             $.w.style.color = $.a.color;
  155.                         }
  156.                         theScripts[i].parentNode.insertBefore($.w, theScripts[i]);
  157.                         theScripts[i].parentNode.removeChild(theScripts[i]);
  158.                         break;
  159.                     }
  160.                 }
  161.             },
  162.             parseJson: function (json) {
  163.                 this.parseJson.data = json;
  164.                 if (typeof json !== 'string') {
  165.                     return {
  166.                         "err": "trying to parse a non-string JSON object"
  167.                     };
  168.                 }
  169.                 try {
  170.                     var f = Function(['var document,top,self,window,parent,Number,Date,Object,Function,',
  171.                         'Array,String,Math,RegExp,Image,ActiveXObject;',
  172.                         'return (', json.replace(/<\!--.+-->/gim, '').replace(/\bfunction\b/g, 'function­'), ');'
  173.                     ].join(''));
  174.                     return f();
  175.                 } catch (e) {
  176.                     return {
  177.                         "err": "trouble parsing JSON object"
  178.                     };
  179.                 }
  180.             }
  181.         };
  182.     }();
  183.     var thisScript = /behavior.js/;
  184.     var thisScriptProduction = /^https?:\/\/[^\/]*yourdomain.com\/yourpath\/behavior.js$/;
  185.     if (typeof window.addEventListener !== 'undefined') {
  186.         window.addEventListener('load', function () {
  187.             $.f.init(thisScript);
  188.         }, false);
  189.     } else if (typeof window.attachEvent !== 'undefined') {
  190.         window.attachEvent('onload', function () {
  191.             $.f.init(thisScript);
  192.         });
  193.     }
  194. })();
  195.  
  196.  
  197. // STAGE VII
  198.  
  199. (function () {
  200.     var trueName = '';
  201.     for (var i = 0; i < 16; i++) {
  202.         trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
  203.     }
  204.     window[trueName] = {};
  205.     var $ = window[trueName];
  206.     $.f = function () {
  207.         return {
  208.             init: function (target) {
  209.                 var theScripts = document.getElementsByTagName('SCRIPT');
  210.                 for (var i = 0; i < theScripts.length; i++) {
  211.                     if (theScripts[i].src.match(target)) {
  212.                         $.w = document.createElement('DIV');
  213.                         $.a = {};
  214.                         if (theScripts[i].innerHTML) {
  215.                             $.a = $.f.parseJson(theScripts[i].innerHTML);
  216.                         }
  217.                         if ($.a.err) {
  218.                             alert('bad json!');
  219.                         }
  220.                         $.w.q = document.createElement('INPUT');
  221.                         if ($.a.query) {
  222.                             $.w.q.value = $.a.query;
  223.                         }
  224.                         $.w.q.onkeypress = function (e) {
  225.                             if ((e ? e.which : event.keyCode) == 13) {
  226.                                 $.f.runSearch();
  227.                             }
  228.                         };
  229.                         $.w.appendChild($.w.q);
  230.                         $.w.b = document.createElement('BUTTON');
  231.                         $.w.b.innerHTML = 'Search';
  232.                         if ($.a.site) {
  233.                             $.w.b.innerHTML += ' ' + $.a.site;
  234.                         }
  235.                         $.w.b.onmouseup = function () {
  236.                             $.f.runSearch();
  237.                         };
  238.                         $.w.appendChild($.w.b);
  239.                         $.w.r = document.createElement('UL');
  240.                         $.w.appendChild($.w.r);
  241.                         theScripts[i].parentNode.insertBefore($.w, theScripts[i]);
  242.                         theScripts[i].parentNode.removeChild(theScripts[i]);
  243.                         break;
  244.                     }
  245.                 }
  246.             },
  247.             runSearch: function () {
  248.                 $.w.r.innerHTML = '';
  249.                 if ($.w.q.value) {
  250.                     if (!$.f.runFunction) {
  251.                         $.f.runFunction = [];
  252.                     }
  253.                     var n = $.f.runFunction.length;
  254.                     var id = trueName + '.f.runFunction[' + n + ']';
  255.                     $.f.runFunction[n] = function (r) {
  256.                         delete($.f.runFunction[n]);
  257.                         $.f.removeScript(id);
  258.                         $.f.renderResult(r);
  259.                     }
  260.                     var url = 'http://search.yahooapis.com/WebSearchService/V1/webSearch?';
  261.                     url += '&appid=YahooSearch';
  262.                     url += '&results=5';
  263.                     url += '&output=json';
  264.                     url += '&query=' + $.w.q.value;
  265.                     url += '&callback=' + id;
  266.                     if ($.a.site) {
  267.                         url += '&site=' + $.a.site;
  268.                     }
  269.                     $.f.runScript(url, id);
  270.                 }
  271.             },
  272.             renderResult: function (r) {
  273.                 for (var i = 0; i < r.ResultSet.Result.length; i++) {
  274.                     var li = document.createElement('LI');
  275.                     var a = document.createElement('A');
  276.                     a.innerHTML = r.ResultSet.Result[i].Title;
  277.                     a.href = r.ResultSet.Result[i].Url;
  278.                     a.target = '_blank';
  279.                     li.appendChild(a);
  280.                     $.w.r.appendChild(li);
  281.                 }
  282.             },
  283.             runScript: function (url, id) {
  284.                 var s = document.createElement('script');
  285.                 s.id = id;
  286.                 s.type = 'text/javascript';
  287.                 s.src = url;
  288.                 document.getElementsByTagName('body')[0].appendChild(s);
  289.             },
  290.             removeScript: function (id) {
  291.                 var s = '';
  292.                 if (s = document.getElementById(id)) {
  293.                     s.parentNode.removeChild(s);
  294.                 }
  295.             },
  296.             parseJson: function (json) {
  297.                 this.parseJson.data = json;
  298.                 if (typeof json !== 'string') {
  299.                     return {
  300.                         "err": "trying to parse a non-string JSON object"
  301.                     };
  302.                 }
  303.                 try {
  304.                     var f = Function(['var document,top,self,window,parent,Number,Date,Object,Function,',
  305.                         'Array,String,Math,RegExp,Image,ActiveXObject;',
  306.                         'return (', json.replace(/<\!--.+-->/gim, '').replace(/\bfunction\b/g, 'function­'), ');'
  307.                     ].join(''));
  308.                     return f();
  309.                 } catch (e) {
  310.                     return {
  311.                         "err": "trouble parsing JSON object"
  312.                     };
  313.                 }
  314.             }
  315.         };
  316.     }();
  317.     var thisScript = /behavior.js/;
  318.     var thisScriptProduction = /^https?:\/\/[^\/]*yourdomain.com\/yourpath\/behavior.js$/;
  319.     if (typeof window.addEventListener !== 'undefined') {
  320.         window.addEventListener('load', function () {
  321.             $.f.init(thisScript);
  322.         }, false);
  323.     } else if (typeof window.attachEvent !== 'undefined') {
  324.         window.attachEvent('onload', function () {
  325.             $.f.init(thisScript);
  326.         });
  327.     }
  328. })();
  329.  
  330.  
  331. // STAGE VIII
  332.  
  333. (function () {
  334.     var trueName = '';
  335.     for (var i = 0; i < 16; i++) {
  336.         trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
  337.     }
  338.     window[trueName] = {};
  339.     var $ = window[trueName];
  340.     $.f = function () {
  341.         return {
  342.             init: function (target) {
  343.                 var theScripts = document.getElementsByTagName('SCRIPT');
  344.                 for (var i = 0; i < theScripts.length; i++) {
  345.                     if (theScripts[i].src.match(target)) {
  346.                         $.w = document.createElement('DIV');
  347.                         $.a = {};
  348.                         if (theScripts[i].innerHTML) {
  349.                             $.a = $.f.parseJson(theScripts[i].innerHTML);
  350.                         }
  351.                         if ($.a.err) {
  352.                             alert('bad json!');
  353.                         }
  354.                         $.d = {
  355.                             "background": "#fff",
  356.                             "border": "1px solid #000"
  357.                         };
  358.                         for (var k in $.d) {
  359.                             if ($.a[k] === undefined) {
  360.                                 $.a[k] = $.d[k];
  361.                             }
  362.                         }
  363.                         var ns = document.createElement('style');
  364.                         document.getElementsByTagName('head')[0].appendChild(ns);
  365.                         if (!window.createPopup) {
  366.                             ns.appendChild(document.createTextNode(''));
  367.                             ns.setAttribute("type", "text/css");
  368.                         }
  369.                         var s = document.styleSheets[document.styleSheets.length - 1];
  370.                         var rules = {
  371.                             "": "{zoom:1;padding:5px;margin:5px;background:" + $.a.background + ";border:" + $.a.border + "}",
  372.                             "button": "{margin-left:5px;}",
  373.                             "ul": "{margin:0; padding:0;}",
  374.                             "ul li": "{list-style:none;}",
  375.                         };
  376.                         var ieRules = "";
  377.                         for (r in rules) {
  378.                             var selector = '.' + trueName + ' ' + r;
  379.                             if (!window.createPopup) {
  380.                                 var theRule = document.createTextNode(selector + rules[r]);
  381.                                 ns.appendChild(theRule);
  382.                             } else {
  383.                                 ieRules += selector + rules[r];
  384.                             }
  385.                         }
  386.                         if (window.createPopup) {
  387.                             s.cssText = ieRules;
  388.                         }
  389.                         $.w.className = trueName;
  390.                         $.w.q = document.createElement('INPUT');
  391.                         if ($.a.query) {
  392.                             $.w.q.value = $.a.query;
  393.                         }
  394.                         $.w.q.onkeypress = function (e) {
  395.                             if ((e ? e.which : event.keyCode) == 13) {
  396.                                 $.f.runSearch();
  397.                             }
  398.                         };
  399.                         $.w.appendChild($.w.q);
  400.                         $.w.b = document.createElement('BUTTON');
  401.                         $.w.b.innerHTML = 'Search';
  402.                         if ($.a.site) {
  403.                             $.w.b.innerHTML += ' ' + $.a.site;
  404.                         }
  405.                         $.w.b.onmouseup = function () {
  406.                             $.f.runSearch();
  407.                         };
  408.                         $.w.appendChild($.w.b);
  409.                         $.w.r = document.createElement('UL');
  410.                         $.w.appendChild($.w.r);
  411.                         theScripts[i].parentNode.insertBefore($.w, theScripts[i]);
  412.                         theScripts[i].parentNode.removeChild(theScripts[i]);
  413.                         break;
  414.                     }
  415.                 }
  416.             },
  417.             runSearch: function () {
  418.                 $.w.r.innerHTML = '';
  419.                 if ($.w.q.value) {
  420.                     if (!$.f.runFunction) {
  421.                         $.f.runFunction = [];
  422.                     }
  423.                     var n = $.f.runFunction.length;
  424.                     var id = trueName + '.f.runFunction[' + n + ']';
  425.                     $.f.runFunction[n] = function (r) {
  426.                         delete($.f.runFunction[n]);
  427.                         $.f.removeScript(id);
  428.                         $.f.renderResult(r);
  429.                     }
  430.                     var url = 'http://search.yahooapis.com/WebSearchService/V1/webSearch?';
  431.                     url += '&appid=YahooSearch';
  432.                     url += '&results=5';
  433.                     url += '&output=json';
  434.                     url += '&query=' + $.w.q.value;
  435.                     url += '&callback=' + id;
  436.                     if ($.a.site) {
  437.                         url += '&site=' + $.a.site;
  438.                     }
  439.                     $.f.runScript(url, id);
  440.                 }
  441.             },
  442.             renderResult: function (r) {
  443.                 for (var i = 0; i < r.ResultSet.Result.length; i++) {
  444.                     var li = document.createElement('LI');
  445.                     var a = document.createElement('A');
  446.                     a.innerHTML = r.ResultSet.Result[i].Title;
  447.                     a.href = r.ResultSet.Result[i].Url;
  448.                     a.target = '_blank';
  449.                     li.appendChild(a);
  450.                     $.w.r.appendChild(li);
  451.                 }
  452.             },
  453.             runScript: function (url, id) {
  454.                 var s = document.createElement('script');
  455.                 s.id = id;
  456.                 s.type = 'text/javascript';
  457.                 s.src = url;
  458.                 document.getElementsByTagName('body')[0].appendChild(s);
  459.             },
  460.             removeScript: function (id) {
  461.                 var s = '';
  462.                 if (s = document.getElementById(id)) {
  463.                     s.parentNode.removeChild(s);
  464.                 }
  465.             },
  466.             parseJson: function (json) {
  467.                 this.parseJson.data = json;
  468.                 if (typeof json !== 'string') {
  469.                     return {
  470.                         "err": "trying to parse a non-string JSON object"
  471.                     };
  472.                 }
  473.                 try {
  474.                     var f = Function(['var document,top,self,window,parent,Number,Date,Object,Function,',
  475.                         'Array,String,Math,RegExp,Image,ActiveXObject;',
  476.                         'return (', json.replace(/<\!--.+-->/gim, '').replace(/\bfunction\b/g, 'function­'), ');'
  477.                     ].join(''));
  478.                     return f();
  479.                 } catch (e) {
  480.                     return {
  481.                         "err": "trouble parsing JSON object"
  482.                     };
  483.                 }
  484.             }
  485.         };
  486.     }();
  487.     var thisScript = /behavior.js/;
  488.     var thisScriptProduction = /^https?:\/\/[^\/]*yourdomain.com\/yourpath\/behavior.js$/;
  489.     if (typeof window.addEventListener !== 'undefined') {
  490.         window.addEventListener('load', function () {
  491.             $.f.init(thisScript);
  492.         }, false);
  493.     } else if (typeof window.attachEvent !== 'undefined') {
  494.         window.attachEvent('onload', function () {
  495.             $.f.init(thisScript);
  496.         });
  497.     }
  498. })();
  499.  
  500. //<script src="behavior.js"></script>
  501. //<script src="behavior.js">{"site":"en.wikipedia.org", "background":"#ffa"}</script>
  502. //<script src="behavior.js">{"query":"madonna", "site":"mtv.com", "border":"1px solid red"}</script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement