By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

pre-loading images not working?

P: n/a
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
Sep 14 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
> if (document.images)

what is this ?
Sep 14 '05 #2

P: n/a
Zoe Brown said the following on 9/14/2005 6:24 AM:
if (document.images)

what is this ?


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

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 14 '05 #3

P: n/a

"Randy Webb" <Hi************@aol.com> wrote in message
news:LY********************@comcast.com...
Zoe Brown said the following on 9/14/2005 6:24 AM:
if (document.images)

what is this ?


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


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 !
Sep 14 '05 #4

P: n/a
ASM
Geoff Cox wrote:
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[i] = new Image(w,h);
S[i].onload = store_pics;
S[i].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="">
--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #5

P: n/a
On Wed, 14 Sep 2005 06:56:56 -0400, Randy Webb
<Hi************@aol.com> wrote:
Zoe Brown said the following on 9/14/2005 6:24 AM:
if (document.images)

what is this ?


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

Sep 14 '05 #6

P: n/a
ASM
Zoe Brown wrote:
if (document.images)

what is this ?


the img collection of page

as you have

document.forms
document.links
--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #7

P: n/a
On Wed, 14 Sep 2005 10:56:59 GMT, "Zoe Brown"
<zo***********@N-O-S-P-A-A-Mtesco.net> wrote:

"Randy Webb" <Hi************@aol.com> wrote in message
news:LY********************@comcast.com...
Zoe Brown said the following on 9/14/2005 6:24 AM:
if (document.images)
what is this ?


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


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
Sep 14 '05 #8

P: n/a

"Geoff Cox" <ge*******@notquitecorrectfreeuk.com> wrote in message
news:5i********************************@4ax.com...
On Wed, 14 Sep 2005 06:56:56 -0400, Randy Webb
<Hi************@aol.com> wrote:
Zoe Brown said the following on 9/14/2005 6:24 AM:
if (document.images)
what is this ?


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?


I am wondering how you *know* they are not being downloaded ?

Please stick that alert in and check that your code is doing something
please.
Sep 14 '05 #9

P: n/a
ASM
Zoe Brown wrote:
"Randy Webb" <Hi************@aol.com> wrote in message
news:LY********************@comcast.com...
Zoe Brown said the following on 9/14/2005 6:24 AM:
if (document.images)

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


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
--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #10

P: n/a
ASM
Geoff Cox wrote:
On Wed, 14 Sep 2005 06:56:56 -0400, Randy Webb
<Hi************@aol.com> wrote:

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

if (document.images)
what is this ?


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?


see my other posts ... (above)

--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #11

P: n/a
On Wed, 14 Sep 2005 10:56:59 GMT, "Zoe Brown"
<zo***********@N-O-S-P-A-A-Mtesco.net> wrote:

"Randy Webb" <Hi************@aol.com> wrote in message
news:LY********************@comcast.com...
Zoe Brown said the following on 9/14/2005 6:24 AM:
if (document.images)
what is this ?


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


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


Sep 14 '05 #12

P: n/a
On Wed, 14 Sep 2005 11:28:17 GMT, Geoff Cox
<ge*******@notquitecorrectfreeuk.com> wrote:
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.
Sep 14 '05 #13

P: n/a
ASM
Geoff Cox wrote:
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
<43**********************@news.wanadoo.fr>

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 !)

--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #14

P: n/a
On Wed, 14 Sep 2005 11:40:51 GMT, ji*@jibbering.com (Jim Ley) wrote:
On Wed, 14 Sep 2005 11:28:17 GMT, Geoff Cox
<ge*******@notquitecorrectfreeuk.com> wrote:
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.

Jim and others - thanks for all your comments..

Cheers

Geoff
Sep 14 '05 #15

P: n/a
Geoff Cox wrote:
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

Sep 14 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.