pre-loading images not working?

G

Geoff Cox

Hello

I have following type of code in the header

function pre_load_pics()
{
if (document.images)
{
var image1 = new Image(400,265);
image1.scr = "pic1.jpg";
var image2 = new Image(400,265);
image2.scr = "pic2.jpg";

etc etc

}

and the following in the body

<script>
pre_load_pics();
</script>

but images are not downloaded until the app needs them later.
Something wrong ? I thought they should be downloaded earlier?

Cheers

Geoff
 
R

Randy Webb

Zoe Brown said the following on 9/14/2005 6:24 AM:
what is this ?

It is a test to see if the browser supports the document.images
collection, when the test should be window.Images
 
A

ASM

Geoff said:
Hello

I have following type of code in the header

function pre_load_pics()
{
if (document.images)
{
var image1 = new Image(400,265);

no !
not var imag...

you need global variables
if not you'll can't call them !
image1.scr = "pic1.jpg";
var image2 = new Image(400,265);
image2.scr = "pic2.jpg";

etc etc

}

and the following in the body

<script>
pre_load_pics();
</script>

but images are not downloaded until the app needs them later.
Something wrong ? I thought they should be downloaded earlier?

try :
onload = pre_load_pics;

that would have to post load your images
.... if you don't click on page during loading
(look to your status bar)

anyway ...
your script is only a declaration giving shortcuts for some images
If navigator isn't too lazy or buzzy, perhaps will he store these
images in his cache
and ... of course you must wait complete post-load
before to use images from cache

You can try to force navigator to store images in its cache :
(not verified, and not sure IE and/or Opera will appreciate)

<script type="text/javascript">
var I = new Array();
I[0] = 'pic1.jpg,400,265';
I[1] = 'pic2.jpg,300,225';
// etc

var im = 0;
var S = new Array();

function store_pics() {
if (document.images) {
var sr = I[im].split(',');
var w = sr[1];
var h = sr[2];
S = new Image(w,h);
S.onload = store_pics;
S.scr = sr[0];
im++;
}
}

onload = store_pics;

function show_i(imag) {
if(imag <= im)
document.images('here').src = S[imag].src;
else
alert('image not yet donwloaded\nretry');
}
</script>

<a href="pic1.jpg" target="myImage"
onclick="show_i(0);return false;">image 1</a>
<a href="pic1.jpg" target="myImage"
onclick="show_i(1);return false;">image 2</a>
<a href="pic1.jpg" target="myImage"
onclick="show_i(2);return false;">image 3</a>
<img name="here" src="">
 
G

Geoff Cox

Zoe Brown said the following on 9/14/2005 6:24 AM:


It is a test to see if the browser supports the document.images
collection, when the test should be window.Images

Randy,

OK - I have changed to window.Images but still the images are not
downloaded at the beginning! Why is this?

Cheers

Geoff
 
G

Geoff Cox

Thats what I thought, so Geoff this value will be false and this code is
simply not executing, hence why your images are not being loaded...

you could have tried to debug this yourself and put in an alert to check
that your code was being hit !

I take your point Zoe, but having changed to window.Images the images
are still not being downloaded at the beginning - will try an alert()!

Cheers

Geoff
 
A

ASM

Zoe said:
Thats what I thought, so Geoff this value will be false and this code is
simply not executing, hence why your images are not being loaded...

but as every non-text navigator understand : document.images ...
you could have tried to debug this yourself and put in an alert to check
that your code was being hit !

no it is not this condition which breaks the function

it is overall because the images and images.src are stored in volatile variables
 
G

Geoff Cox

Zoe,

I think I see the problem - the images are being dwonloaded but I am
confusing the situation in the way I am loading them later in the
program...

I have

function pre_load_pics()
{
if (window.Images)
{
alert(window.Images);
var image1 = new Image(400,265);
image1.scr = "pic1.jpg";
var image2 = new Image(400,265);
image2.scr = "pic2.jpg";

etc

but then also

var picture = new Array(7);
picture[0] = "pic1.jpg";
picture[1] = "pic2.jpg";
picture[2] = "pic3.jpg";
picture[3] = "pic4.jpg";

so that I can refer to the pics as picture[situation_number]

How do I correct this?

Cheers

Geoff
 
J

Jim Ley

I think I see the problem - the images are being dwonloaded but I am
confusing the situation in the way I am loading them later in the
program...

Nope, how you refer to a url in script makes no difference.

document.images is perfectly fine, window.Image would also be fine,
window.Images probably not, however your problem is almost certainly
dealt with in the FAQ http://jibbering.com/faq/#FAQ4_31

Jim.
 
A

ASM

Geoff said:
Zoe,

I think I see the problem - the images are being dwonloaded but I am
confusing the situation in the way I am loading them later in the
program...

not only :
- you use volatile variables
- pre load function use real url
- the array of images is not used in this function

see my other post :
'Re: pre-loading images not working'
Wed, 14 Sep 2005 13:14:41 +0200
<[email protected]>

where array of images is used

and it is not important if array of images is set in end of page
if you start the post-loader-function after page is loaded
(and if you don't try to use them during loading ... of course !)
 
M

Mick White

Geoff said:
Hello

I have following type of code in the header

function pre_load_pics()
{
if (document.images)
{
var image1 = new Image(400,265);
image1.scr = "pic1.jpg";
var image2 = new Image(400,265);
image2.scr = "pic2.jpg";

etc etc

}

Perhaps you mean "image1.src", not "image1.scr"
Mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top