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 TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Preventing multiple downloads of same image to be displayed in several places

    Hello,

    So I need to create an image with Javacript: var img = new Image().
    Its source will change every now and then. The catch is, I need to display that image in several places on my page using document.write. All I can think of is

    Code:
    document.write('<img id="test1" src="'+img.src+'");
    ...
    document.write('<img id="test2" src="'+img.src+'");
    The problem is, the browser loads a copy of img each time I try to display it in these placeholders. On my particular page, I have up to 36 copies of that image that I need to display, so that it downloads 36 copies of it! I know there is an easier way to do this. Any help would be much appreciate. The applicable page is

    www.trippertreats.com/Products/Teachers/


    Thanks!
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Try it this way

    PHP Code:
    <HTML>
    <
    HEAD>
    <
    TITLE>Document Title</TITLE>

    <
    script type="text/javascript">
    myImage=new Image()
    myImage.src="image.jpg"

    function loadPics(){
    for(var 
    i=0;i<5;i++){  // change 5 to 36
    document.images["pic"+i].src=myImage.src
    }
    }

    </script>

    </HEAD>
    <BODY onload="loadPics()">

    5 of the 36 images<br>

    <img name="pic0" src="blank.gif">
    <img name="pic1" src="blank.gif">
    <img name="pic2" src="blank.gif">
    <img name="pic3" src="blank.gif">
    <img name="pic4" src="blank.gif">

    </BODY>
    </HTML> 

    Create a 1px X 1px blank gif to use as default
    Last edited by Mr J; 12-20-2006 at 02:22 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    excellent. you just increased this page's download speed 36-fold!

    much obliged.
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."


  •  

    Posting Permissions

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