title: Access local variable from another function
body: <p>I want to do the following in my script:</p>
  
  <p>Declare 2 variables in one function and call this function on window load. After that use values of those 2 variables in my other functions. Any option to do this without having to declare those variables outside the function as globals?</p>
  
  <p>Again, it is important that the variables are declared inside a function which will be called on page load.</p>
  
  <p>HTML code:</p>
  
  <pre><code>&lt;a href=''onclick='_default();return false;'&gt;Link&lt;/a&gt;
  &lt;div id='mydiv'&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>function _default() {
      var x = document.getElementById('mydiv');
      var h = x.offsetHeight;
      _main(x, h);
  }
  _default();
  
  function _main(x, h) {
      alert(x);
      alert(h);
  }
  </code></pre>
  
  <p>You will see that this script executes <code>_default()</code> on page load. <code>_default()</code> function passes 2 parameters to <code>_main()</code> and executes <code>_main()</code> function throwing 2 alerts with variables <code>x</code> and <code>h</code>. But how do I make it so that those 2 alerts are thrown when I click on the link (see html code above)?</p>