title: Javascript global variable not working
body: <p>I've seen a few versions of this question on here, but none of them seem to be the same as my issue, so figured I'd create a new question.  I must be doing something very silly here, but for the life of me I cannot spot it.</p>
  
  <p>I'll start with the context, although it is not particularly relevant: I am deferring loading of jQuery with js code, which means none of my code files can contain a <code>$(document).ready(...)</code> call, because "$" will be undefined at first pass. Instead I am adding my ready functions to a "global" array, and after jQuery loads asynchronously, I call all these functions with the following:</p>
  
  <pre><code>function FireLoadEvents() {
    while ( OnLoadEvents.length &gt; 0 ) {
      var item = OnLoadEvents.splice(0,1);
      item[0].ready();
    }
  }
  </code></pre>
  
  <p>Now in any of my page components I can use the following to add an event I wish to fire when jQuery is loaded and the document is actually ready:</p>
  
  <pre><code>OnLoadEvents.push( { ready: function() { ... } } );
  </code></pre>
  
  <p>I've defined the <code>OnLoadEvents</code> variable at the very top of my template in the head:</p>
  
  <pre><code>&lt;script language="text/javascript"&gt;
  var OnLoadEvents = new Array();
  &lt;/script&gt;
  </code></pre>
  
  <p>As you can see it is outside of any function scope, and it is also declared before it is referenced.  When tested on its own, this setup is working great, but for some reason when it is added to my actual page heads, script tags later in the page are not able to add items to the <code>OnLoadEvents</code> variable.  For instance the following is pulled into my template by a php-include server side, and adds these lines to the body of my page:</p>
  
  <pre><code>&lt;script type='text/javascript'&gt;
  OnLoadEvents.push( { ready: function() {
    if ( $('.sponsor-link').length != 0 ) {
      ... 
    }
  } } );
  &lt;/script&gt;
  </code></pre>
  
  <p>When I hit this page in Chrome, it reports the following exception on the "push" line above:</p>
  
  <pre><code>Uncaught ReferenceError: OnLoadEvents is not defined
  </code></pre>
  
  <p>Any idea what is going on? Why would OnLoadEvents be undefined just a few lines down from where I initialize it?  I'll note that I've also tried defining and referencing <code>OnLoadEvents</code> as <code>window.OnLoadEvents</code>.</p>
  
  <p>Thanks in advance!</p>