Click to See Complete Forum and Search --> : Load an image with JS


o35_jimmy
09-29-2003, 06:00 AM
Hi

I've an html page with approx. this code :

<HTML>
<HEAD>
<script language=javascript>
function loadImg(path) {
path = "../../img/TL/" + path ;
imm0=new Image();
imm0.src=path;
document.images.immagine.src=imm0.src;
}
</script>
</HEAD>
<BODY>
...
<a href="javascript:loadImg('TL0196.jpg')" >196</A>
<a href="javascript:loadImg('TL0197.jpg')" >197</A>
<a href="javascript:loadImg('TL0198.jpg')" >198</A>
...

<img src="loading.gif" name="immagine" border=2>

</BODY>
</HTML>

I want that an image would be loaded in the same page by clicking corresponding number
using js.
In local mode this work well, but once uploaded files on my website this does'not work or
works rarely.
Paths are correct.
Could be a problem about more long time in loading an image by JS ?
Thanks in advance.

Gollum
09-29-2003, 07:32 AM
I'm not sure that URL is correct...
are you sure "../../img/TL/" is contained within the same web site?

o35_jimmy
09-29-2003, 07:37 AM
Yes.
Some times script works.
Now reading some subject with same problem i'm convinced
that it's a synchronization problem..
Somebody use a sleep function for waiting an image to load, but i don't know where to put sleep function...

Gollum
09-29-2003, 08:09 AM
how 'bout instead of

imm0=new Image();
imm0.src=path;
document.images.immagine.src=imm0.src;

you do

document.images.immagine.src=path;

o35_jimmy
09-29-2003, 08:21 AM
Already done.
I've also tried 2 new solutions (with no success):

//**** SOL. 1 ******************
var imm0;
function loadImg(path,descr) {
redy = false;
path = "../../img/TL/TL0001/" + path;
imm0=new Image();
imm0.src=path;
imm0.onLoad = loadImg2() ;
}

function loadImg2() {
document.images.immagine.src=imm0.src;
}

//****SOL 2. **************

var imm0;
var redy;
function loadImg(path,descr) {
redy = false;
path = "../../img/TL/TL0001/" + path;
imm0=new Image();
imm0.src=path;
checkready();
}

function checkready() {
redy = imm0.complete;
if(redy == true) { document.images.immagine.src=imm0.src;
} else {
setTimeout('checkready();',500);
}
}

Gollum
09-29-2003, 08:59 AM
Try chaning SOL.1 to...

function loadImg(path,descr) {
redy = false;
path = "../../img/TL/TL0001/" + path;
var imm0=new Image();
imm0.onload = loadImg2; // no parentheses (and all lower case)
imm0.onerror = failedImg2; // also no parentheses
imm0.src=path; // do this last
}

function loadImg2() {
document.images.immagine.src=this.src;
}

function failedImg2() {
alert("couldn't load image: " + this.src);
}

o35_jimmy
09-29-2003, 09:16 AM
OK !
It works fine !
Thanks a lot !