Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    78
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Easy preload image

    Ok, I'm sure this will only take half a second. I remember seeing this somewhere. What I want is to load "pictures/navbarbg2.jpg" in javascript. I have a image swap on a picture, and when I hover over one picture, there is a blink where the image is loading. If I have javascript put the hover image on cache, I think that will remove the blink.

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72 W. 48' 57" , 41 N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    there're about a million scripts for this, but here's mine:

    Code:
    function preLoad(args){
    	if (document.images){
    		var img = new Image();
    		var failed = [];
    		img.onerror = function(){
    			failed[failed.length] = this.src;
    			if(args.length > 0){ this.src = args.shift(); }
    		}
    		img.onload = function(){
    			if(args.length > 0){ this.src = args.shift(); }
    		}
    		img.src = args.shift();
    	}
    	else { var failed = args; }
    	return failed;
    }
    use it like this:
    Code:
    preLoad(["pictures/navbarbg2.jpg"]);
    preLoad(["preloading.jpg", "multiple.png", "files.gif"]);
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    78
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Well, it seemed to work (always hard to tell). Thanks a lot.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •