title: jquery variable scope
body: <p>I have this code in jquery:</p>
  
  <pre><code>var ratio;
  $("&lt;img/&gt;").attr("src", $(img).attr("src")).load(function() {
  ratio = this.width / this.height;
      alert(ratio);
  });
  </code></pre>
  
  <p>This retuns the correct ratio, but when I try this </p>
  
  <pre><code>var ratio;
  $("&lt;img/&gt;").attr("src", $(img).attr("src")).load(function() {
  ratio = this.width / this.height;        
  });
  alert(ratio);
  </code></pre>
  
  <p>It returns <em>undefined</em>. </p>
  
  <p>I do not understand why this happens even though I have declared globally the ratio variable.</p>
  
  <p>I use this in the following exemple: </p>
  
  <p><strong>working</strong> <a href="http://jsfiddle.net/7sUAY/5/" rel="nofollow">http://jsfiddle.net/7sUAY/5/</a></p>
  
  <p><strong>not working</strong> <a href="http://jsfiddle.net/7sUAY/6/" rel="nofollow">http://jsfiddle.net/7sUAY/6/</a></p>
  
  <p>Thank you,
  Mugur</p>