How do I get 'Content-Length' via javascript?
<p>I have the following snippet:</p>

<pre><code>var size;
function getImageSize(url) 
{
    var xhr = new XMLHttpRequest();
    xhr.open('HEAD', url, true);
    xhr.onreadystatechange = function() 
    {
        if (xhr.readyState == 4) 
        {
            if (xhr.status == 200) 
            {
                size = xhr.getResponseHeader('Content-Length');
                alert(size);
            }
        }
    };
    xhr.send(null);
    return size;
}
</code></pre>

<p>The purpose of the function is to return the file size (of a image).</p>

<p>The alert inside the function will always return the correct value, however the function itself will always return <code>undefined</code>. Why? </p>

<p>Odly, if I debug it with firebug, the function will always return correctly...</p>
