...

View Full Version : why on earth wont this work



gerble1000
07-01-2012, 01:33 PM
i have a simple image swap code here


<SCRIPT language="JavaScript">
function movepic(img_name,img_src)
{
objImage = new Image();
objImage.onLoad=imagesLoaded();
objImage.src=img_src;
function imagesLoaded()
{
document[img_name].src=img_src;
}
}
</SCRIPT>

that code works lovely.
now i want to display a loading.gif while its loading.
here is my new code


<SCRIPT language="JavaScript">
function movepic(img_name,img_src)
{
document.getElementById("loading").style.display="block";
objImage = new Image();
objImage.onLoad=imagesLoaded();
objImage.src=img_src;

function imagesLoaded()
{
document[img_name].src=img_src;
document.getElementById("loading").style.display="none";
}
}
</SCRIPT>

either it does not set the loading to visible or it does and then instantly makes it not visible but either way its not working.
the code below works fine
document.getElementById("loading").style.display="block";
so please tell me what i am doing wrong.

the code gets executed from an onclick div inside a table td

vwphillips
07-01-2012, 02:03 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
function movepic(img_name,img_src)
{
document.getElementById("loading").style.display="block";
var objImage = new Image();
objImage.onload=function(){ imagesLoaded(img_name,this.src); }
objImage.src=img_src;

function imagesLoaded(img_name,src)
{
document[img_name].src=src;
document.getElementById("loading").style.display="none";
}
}

/*]]>*/
</script></head>

<body>
<img id="loading" src="http://www.vicsjavascripts.org.uk/StdImages/One.gif" />
<img name="img1" src="http://www.vicsjavascripts.org.uk/StdImages/One.gif" />
<input type="button" name="" value="Test" onmouseup="movepic('img1','http://www.vicsjavascripts.org.uk/StdImages/Two.gif')"/>
</body>

</html>

gerble1000
07-01-2012, 02:16 PM
your like a program.
you just fix the problem with no comments.
thankyou works great



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum