problem with Image src loading

problem with Image src loading

I have script:

    var nick = chat_jmeno + '.gif';
    img = new Image();
    img.src = nick;
    if (img.height != 0) {
        $('#avatar').attr ('src', nick);
    }








which changes source of image in <img id="avatar src="picture.gif">, but it works in every browser only sometimes. For example in first load of page not, but sometimes after refreshing page, sometimes not.
I have feeling, that problem is with speed of loading image to the object or something with cache, but don't know. Firebug console in Firefox does not show any error. Only Chrome development console shows

Resource interpreted as Image but transferred with MIME type text/html: "http://addres...".



Has anybody idea, whats wrong?