SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Former Staff Member silver trophy Adam P.'s Avatar
    Join Date
    Apr 2001
    Location
    San Francisco
    Posts
    3,288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What's the JavaScript code for preloading images?

    I found it here:
    http://hotwired.lycos.com/webmonkey/...user_interface
    but, it was kind of confusing, I hate copying and pasting code with comments in it.

    Here's what's on top of my page so far:

    Code:
    <script language="Javascript">
    <!--
    if (document.images) {
    oneup = new Image
    oneover = new Image
    twoup = new Image
    twoover = new Image
    threeup = new Image
    threeover = new Image
    fourup = new Image
    fourover = new Image
    fiveup = new Image
    fiveover = new Image
    sixup = new Image
    sixover = new Image
    
    oneup.src = 'index2stuff/1up.gif'
    oneover.src = 'index2stuff/1over.gif'
    twoup.src = 'index2stuff/2up.gif'
    twoover.src = 'index2stuff/2over.gif'
    threeup.src = 'index2stuff/3up.gif'
    threeover.src = 'index2stuff/3over.gif'
    fourup.src = 'index2stuff/4up.gif'
    fourover.src = 'index2stuff/4over.gif'
    fiveup.src = 'index2stuff/5up.gif'
    fiveover.src = 'index2stuff/5over.gif'
    sixup.src = 'index2stuff/6up.gif'
    sixover.src = 'index2stuff/6over.gif'
    }
    //-->
    </script>
    I need to preload the 12 images named above.

    What's the code I would have to add inside of here?

    Thanks!
    SPF Mentor/Advisor 2001-2003
    SPF Designer of the Year 2002
    SPF Graphic Designer of the Year 2003
    AdamPolselli.com

  2. #2
    Former Staff Member silver trophy Adam P.'s Avatar
    Join Date
    Apr 2001
    Location
    San Francisco
    Posts
    3,288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone? I'm desperate!!

    pleeaase.....
    SPF Mentor/Advisor 2001-2003
    SPF Designer of the Year 2002
    SPF Graphic Designer of the Year 2003
    AdamPolselli.com

  3. #3
    SitePoint Enthusiast S.K's Avatar
    Join Date
    Mar 2001
    Location
    Chennai,India
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    www.javascriptsource.com gives the following code snippet:

    <SCRIPT LANGUAGE="JavaScript">

    <!-- Begin

    image1 = new Image();
    image1.src = "image1.gif";

    image2 = new Image();
    image2.src = "image2.gif";

    // End -->
    </script>
    S.K
    CyberBrahma
    ~~~~~~~~~~~
    Ignorance is Bliss!

  4. #4
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup...that;s the way. Even your example will work Swimm.

    Basically, what the script is saying is "if the browser understands document.images, then load the following images into a variable.

    In SK's example, the variable is image1. new Image() is the type and "image1.gif" is the cource for image1 (the variable).
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  5. #5
    Former Staff Member silver trophy Adam P.'s Avatar
    Join Date
    Apr 2001
    Location
    San Francisco
    Posts
    3,288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So all I have to add is the ( )s?
    SPF Mentor/Advisor 2001-2003
    SPF Designer of the Year 2002
    SPF Graphic Designer of the Year 2003
    AdamPolselli.com

  6. #6
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like it.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  7. #7
    Former Staff Member silver trophy Adam P.'s Avatar
    Join Date
    Apr 2001
    Location
    San Francisco
    Posts
    3,288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One more thing...

    Should I move around the code so that it's like this?

    image1 = new image()
    image1.src = *****

    Right now I have all of the new images listed, then all of the .src(s) listed.

    Also, do I need the ; after every line?
    SPF Mentor/Advisor 2001-2003
    SPF Designer of the Year 2002
    SPF Graphic Designer of the Year 2003
    AdamPolselli.com

  8. #8
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can list them in whatever order you like. and you don't NEED the ; at the end of statements in JS IF there's a newline. but i like to put them just to be consistent w/ other languages.


Bookmarks

Posting Permissions

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