Jump to content

Recommended Posts

I just made a preload script, but i thinks it's not working, please let me know what i am doing wrong here:

		 if(document.images) {
		imgs = new Array("home_btn_h", "login_btn_h", "reg_btn_h", "del_forum_btn", "msgs_btn_h", "new_forum_btn_h", "reg_btn_h", "unread_btn_h");

		for(var i=0; i<=imgs.size, i++) {
			imgs[i] = new Image(87, 30);
			imgs[i].src="assets/" + imgs[i] + ".gif";
			alert("assets/" + imgs[i] + ".gif");
		}
	}

i used "alert" for testing purposes, that's how i know that it's not working.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/
Share on other sites

 

		 if(document.images) {
		imgs = new Array("home_btn_h", "login_btn_h", "reg_btn_h", "del_forum_btn", "msgs_btn_h", "new_forum_btn_h", "reg_btn_h", "unread_btn_h");

		for(var i=0; i<=imgs.size, i++) {
			imgs[i] = new Image(87, 30);
			imgs[i].src="assets/" + imgs[i] + ".gif";
			alert("assets/" + imgs[i] + ".gif");
		}
	}

 

You have a comma in your loop declaration. Make it

for(var i=0; i<=imgs.length; i++) {

  whatever

}

 

See if that works!

I dont know think it would run right off the bat, you could add it to your <BODY> tag

<BODY ONLOAD=init();>

 

something like that

 

That's an ugly way to do it.  Markup should have no knowledge of the script(s) used upon it.

 

Try, instead:

<html>
<head>
   <title>Test</title>
   <script type="text/javascript">
      window.onload = function()
      {
         var imgs = new Array("home_btn_h", "login_btn_h", "reg_btn_h", "del_forum_btn", "msgs_btn_h", "new_forum_btn_h", "reg_btn_h", "unread_btn_h");

         for(var i = 0; i < imgs.length; i++)
         {
            imgs[i] = new Image(87, 30);
            imgs[i].src="assets/" + imgs[i] + ".gif";
            alert("assets/" + imgs[i] + ".gif");
         }
      }
   </script>
</head>

<body>

<!-- markup -->

</body>
</html>

 

Keep in mind, you may be overwriting your initial image values ("home_btn_h", etc) with the new Image objects.  To be safe, you should have two arrays: one filled with the image names, one you build as you go on:

var imgNames = new Array("home_btn_h", "login_btn_h", "reg_btn_h", "del_forum_btn", "msgs_btn_h", "new_forum_btn_h", "reg_btn_h", "unread_btn_h");
var imgs = new Array();

for(var i = 0; i < imgNames.length; i++)
{
   imgs[i] = new Image(87, 30);
   imgs[i].src="assets/" + imgNames[i] + ".gif";
   alert("assets/" + imgs[i] + ".gif");
}

Thanks, i'll use that method.

 

I finally figured it out, i had few errors:

1) i had <script type="javascript"> when it should have been <script language="javascript">

2) Instead of getting array size like this: "imgNames.length" i used "imgNames.size".

 

Thanks again.

i think he is saying that window.onload is fired after images are already displayed.

Preload is done for hover effects.

 

Ah, good point.  But, to be honest, hardly anyone uses JavaScript for hyperlink hover effects any more anyway.  If it's a bunch of links, just use CSS :hover to do it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.