Connecting Tech Pros Worldwide Help | Site Map

pre-loading images not working?

Geoff Cox
Guest
 
Posts: n/a
#1: Sep 14 '05
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


Zoe Brown
Guest
 
Posts: n/a
#2: Sep 14 '05

re: pre-loading images not working?


> if (document.images)

what is this ?


Randy Webb
Guest
 
Posts: n/a
#3: Sep 14 '05

re: pre-loading images not working?


Zoe Brown said the following on 9/14/2005 6:24 AM:
[color=blue][color=green]
>>if (document.images)[/color]
>
>
> what is this ?[/color]

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
Zoe Brown
Guest
 
Posts: n/a
#4: Sep 14 '05

re: pre-loading images not working?



"Randy Webb" <HikksNotAtHome@aol.com> wrote in message
news:LYmdnRy_46tFnLXeRVn-vQ@comcast.com...[color=blue]
> Zoe Brown said the following on 9/14/2005 6:24 AM:
>[color=green][color=darkred]
>>>if (document.images)[/color]
>>
>>
>> what is this ?[/color]
>
> It is a test to see if the browser supports the document.images
> collection, when the test should be window.Images
>[/color]

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 !


ASM
Guest
 
Posts: n/a
#5: Sep 14 '05

re: pre-loading images not working?


Geoff Cox wrote:[color=blue]
> Hello
>
> I have following type of code in the header
>
> function pre_load_pics()
> {
> if (document.images)
> {
> var image1 = new Image(400,265);[/color]

no !
not var imag...

you need global variables
if not you'll can't call them !
[color=blue]
> 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?[/color]

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
Geoff Cox
Guest
 
Posts: n/a
#6: Sep 14 '05

re: pre-loading images not working?


On Wed, 14 Sep 2005 10:56:59 GMT, "Zoe Brown"
<zoenaomibrown@N-O-S-P-A-A-Mtesco.net> wrote:
[color=blue]
>
>"Randy Webb" <HikksNotAtHome@aol.com> wrote in message
>news:LYmdnRy_46tFnLXeRVn-vQ@comcast.com...[color=green]
>> Zoe Brown said the following on 9/14/2005 6:24 AM:
>>[color=darkred]
>>>>if (document.images)
>>>
>>>
>>> what is this ?[/color]
>>
>> It is a test to see if the browser supports the document.images
>> collection, when the test should be window.Images
>>[/color]
>
>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 ![/color]

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


Geoff Cox
Guest
 
Posts: n/a
#7: Sep 14 '05

re: pre-loading images not working?


On Wed, 14 Sep 2005 06:56:56 -0400, Randy Webb
<HikksNotAtHome@aol.com> wrote:
[color=blue]
>Zoe Brown said the following on 9/14/2005 6:24 AM:
>[color=green][color=darkred]
>>>if (document.images)[/color]
>>
>>
>> what is this ?[/color]
>
>It is a test to see if the browser supports the document.images
>collection, when the test should be window.Images[/color]

Randy,

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

Cheers

Geoff



ASM
Guest
 
Posts: n/a
#8: Sep 14 '05

re: pre-loading images not working?


Zoe Brown wrote:[color=blue][color=green]
>>if (document.images)[/color]
>
>
> what is this ?[/color]

the img collection of page

as you have

document.forms
document.links


--
Stephane Moriaux et son [moins] vieux Mac
Zoe Brown
Guest
 
Posts: n/a
#9: Sep 14 '05

re: pre-loading images not working?



"Geoff Cox" <geoff.cox@notquitecorrectfreeuk.com> wrote in message
news:5i1gi19n27p12ptn9jac4c8a0o7o25b8fo@4ax.com...[color=blue]
> On Wed, 14 Sep 2005 06:56:56 -0400, Randy Webb
> <HikksNotAtHome@aol.com> wrote:
>[color=green]
>>Zoe Brown said the following on 9/14/2005 6:24 AM:
>>[color=darkred]
>>>>if (document.images)
>>>
>>>
>>> what is this ?[/color]
>>
>>It is a test to see if the browser supports the document.images
>>collection, when the test should be window.Images[/color]
>
> Randy,
>
> OK - I have changed to window.Images but still the images are not
> downloaded at the beginning! Why is this?[/color]

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.


ASM
Guest
 
Posts: n/a
#10: Sep 14 '05

re: pre-loading images not working?


Geoff Cox wrote:[color=blue]
> On Wed, 14 Sep 2005 06:56:56 -0400, Randy Webb
> <HikksNotAtHome@aol.com> wrote:
>
>[color=green]
>>Zoe Brown said the following on 9/14/2005 6:24 AM:
>>
>>[color=darkred]
>>>>if (document.images)
>>>
>>>
>>>what is this ?[/color]
>>
>>It is a test to see if the browser supports the document.images
>>collection, when the test should be window.Images[/color]
>
>
> Randy,
>
> OK - I have changed to window.Images but still the images are not
> downloaded at the beginning! Why is this?[/color]

see my other posts ... (above)



--
Stephane Moriaux et son [moins] vieux Mac
ASM
Guest
 
Posts: n/a
#11: Sep 14 '05

re: pre-loading images not working?


Zoe Brown wrote:[color=blue]
> "Randy Webb" <HikksNotAtHome@aol.com> wrote in message
> news:LYmdnRy_46tFnLXeRVn-vQ@comcast.com...
>[color=green]
>>Zoe Brown said the following on 9/14/2005 6:24 AM:
>>[color=darkred]
>>>>if (document.images)
>>>
>>>what is this ?[/color]
>>
>>It is a test to see if the browser supports the document.images
>>collection, when the test should be window.Images[/color]
>
> 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...[/color]

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

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
Geoff Cox
Guest
 
Posts: n/a
#12: Sep 14 '05

re: pre-loading images not working?


On Wed, 14 Sep 2005 10:56:59 GMT, "Zoe Brown"
<zoenaomibrown@N-O-S-P-A-A-Mtesco.net> wrote:
[color=blue]
>
>"Randy Webb" <HikksNotAtHome@aol.com> wrote in message
>news:LYmdnRy_46tFnLXeRVn-vQ@comcast.com...[color=green]
>> Zoe Brown said the following on 9/14/2005 6:24 AM:
>>[color=darkred]
>>>>if (document.images)
>>>
>>>
>>> what is this ?[/color]
>>
>> It is a test to see if the browser supports the document.images
>> collection, when the test should be window.Images[/color][/color]

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




Jim Ley
Guest
 
Posts: n/a
#13: Sep 14 '05

re: pre-loading images not working?


On Wed, 14 Sep 2005 11:28:17 GMT, Geoff Cox
<geoff.cox@notquitecorrectfreeuk.com> wrote:
[color=blue]
>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...[/color]

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.
ASM
Guest
 
Posts: n/a
#14: Sep 14 '05

re: pre-loading images not working?


Geoff Cox wrote:[color=blue]
> 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...[/color]

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
<432806a9$0$7837$8fcfb975@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
Geoff Cox
Guest
 
Posts: n/a
#15: Sep 14 '05

re: pre-loading images not working?


On Wed, 14 Sep 2005 11:40:51 GMT, jim@jibbering.com (Jim Ley) wrote:
[color=blue]
>On Wed, 14 Sep 2005 11:28:17 GMT, Geoff Cox
><geoff.cox@notquitecorrectfreeuk.com> wrote:
>[color=green]
>>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...[/color]
>
>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.[/color]


Jim and others - thanks for all your comments..

Cheers

Geoff
Mick White
Guest
 
Posts: n/a
#16: Sep 14 '05

re: pre-loading images not working?


Geoff Cox wrote:
[color=blue]
> 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
>
> }[/color]

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

Closed Thread