views:

419

answers:

1

Hello, I have spent the whole day trying to make a script which on "submit" hides the form and shows hidden with animated progress bar. The problem is that Internet Explorer doesn't show animated gif images in hidden divs. The images are static. I visited many websites and found a script which uses:

document.getElementById(id).style.backgroundImage = 'url(/images/load.gif)';

Finally, my script works in Internet Explorer, Firefox, Opera but... Google Chrome doesn't display the image at all. I see only div text. After many tests I discovered the following: the only way to see the background image in Google Chrome is to include the same image somewhere in the page (outside of hidden div) with 1px dimensions:

<img src="/images/load.gif" width="1" heigh="1" /> 

This did the trick but... after this dirty solution Microsoft Explorer for some reason shows the image as static again. So, my question is: is there any way how to force Gogle Chrome to show the image? Thanks. This is my script:

<script language="JavaScript" type="text/javascript">

function ver (id, elementId){
if (document.getElementById('espera').style.visibility == "visible") {
    return false;
}else{
var esplit = document.forms[0]['userfile'].value.split(".");
ext = esplit[esplit.length-1];
    if (document.forms[0]['userfile'].value == '') {
        alert('Please select a file');
        return false;
    }else{
        if ((ext.toLowerCase() == 'jpg')) {
            document.getElementById(id).style.position = 'absolute';
            document.getElementById(id).style.display = 'inline';
            document.getElementById(id).style.visibility = "visible";
            document.getElementById(id).style.backgroundImage = 'url(/images/load.gif)';
            document.getElementById(id).style.height = "100px";
            document.getElementById(id).style.backgroundColor = '#f3f3f3';
            document.getElementById(id).style.backgroundRepeat = "no-repeat";
            document.getElementById(id).style.backgroundPosition = "50% 50%";

        var element;
            if (document.all)
                element = document.all[elementId];
            else if (document.getElementById)
                element = document.getElementById(elementId);
            if (element && element.style)
                element.style.display = 'none'; 

            return true;
        }else{
            alert('This is not a jpg file');    
            return false;
        }
    }
}
}  
</script>


<div id="frmDiv">
<form  enctype="multipart/form-data" action="/upload.php" method="post" name="upload3" onsubmit="return ver('espera','frmDiv');">
<input type="hidden" name="max_file_size" value="4194304" />
<table border="0" cellspacing="1" cellpadding="2" width="100%">
<tr bgcolor="#f5f5f5">
<td>File (jpg)</td>
<td>
<input type="file" name="userfile" class="upf" /></td></tr>
<tr bgcolor="#f5f5f5">
<td>&nbsp;</td>
<td>
<input class="upf2" type="submit" name="add" value="Upload" />
</td></tr></table></form>
</div>


<div id="espera" style="display:none;text-align:center;float:left;width:753px;">&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />
&nbsp;<br />Please wait...<br />&nbsp;
</div>
+3  A: 

Try:

(new Image).src="/images/load.gif";

The big G itself uses this technique to pre-load DNS lookups on their homepage.


UPDATE Due to the boggling -1 I received...I need to explain why the above may work to the numbskulls that downvote when they don't understand the possibilities of a given solution.

Google Chrome may very well be "intelligently" choosing which resources to download by analyzing if they will actually be displayed on the page (similar to how browsers don't download every background-image:url(image) in a CSS file). If this is true the the following may also be true: If the time the "load.gif" image is intended to be displayed is LESS than the time it takes for the image to be downloaded then it will appear like the image is not displayed at all (even though it is just being downloaded).

By pre-loading the image using the '(new Image).src ="image.gif";' approach we make sure the image will be ready in the browser's cache and thus immediately available when needed.

As to why Internet Exploder is only showing one frame I'm not sure. There must be other variables in the page causing this behavior (long running script, limit number of loops encoded in the GIF itself, ?).

David Murdoch
-1 Please read the question. He is not having a preload problem. The image simply is not being displayed in Chrome.
Josh Stodola
dkamins
Thank you David Murdoch. Unfortunately, this doesn't solve the problem - shows image in Google Chrome but makes image static in Internet Explorer :(
Guanche
Just discovered that everything works fine in all browsers if I include (new Image).src="/images/load.gif"; in a different page which was accessed by a user before submitting form. I will include it in login page if there are no other solution. Not the best solution but it works.
Guanche
@Guanche. There are many performance issues in your script. You should consider refactoring. Also 'language="JavaScript"' is not needed. Oh, if you'd like to +1 me that would be great! :-)
David Murdoch
Thank you David Murdoch for your help. I don't know Java script and possible performance issues. Usually I use free scripts but this time it was not possible to find a script which works in all browsers. My scripts consists of 2 scripts written by other people - the first part checks if a jpg file is selected and shows the hidden div, the second part hides the form. I have found a discussion about this this problem which is opened 6 years ago and still active: http://www.west-wind.com/Weblog/posts/1227.aspx
Guanche