Image doesn't load until I refresh page. document.write???

Discussion in 'JavaScript' started by osu9400, Mar 17, 2008.

  1. #1
    My wife is professional photographer and I am building a new proof site for her. The proof pages load, but the image is not visible until I refresh it. Does anyone have any idea this would happen?

    Here is a sample page (you can view source on this page): http://www.gentryfoto.com/test/pages/(1013).htm

    Here is the code that loads the image. Keep in mind these values aren't hard coded. They are generated at pubish time in Photoshop.
    http://www.gentryfoto.com/js2.jpg
     
    osu9400, Mar 17, 2008 IP
  2. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The pictures appear fine for me without having to refresh the page in IE7, FF2 and Opera 9.

    Is it possibly the Minefield browser which is to blame?
     
    vpguy, Mar 17, 2008 IP
  3. osu9400

    osu9400 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply. I needed to get this fixed tonight so I removed the dynamic width portion of the site. I now manually set the width and it is bugging. You probably viewed the site after I "fixed" it.

    After viewing the code sample in the jpg, did anything stand out that might be wonrg?
     
    osu9400, Mar 17, 2008 IP
  4. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Oh... bad timing on my part. :p

    I don't really see anything wrong with the code, but it could certainly be shortened. The following code should accomplish the same thing:

    
    var i = new Image();
    i.src = "../images/(1013).jpg";
    var w = Math.min(i.width, 680);
    
    document.write("<a name='13' href='../index.htm#13'><img border='0' src='../images/(1013).jpg' width='" + w + "' alt='(1013)'><br>");
    
    Code (markup):
     
    vpguy, Mar 17, 2008 IP
  5. osu9400

    osu9400 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks. That makes perfect sense. I'll try it and see if it makes any difference. Can you tell I learned to program on the old 3g languages like Pascal!?!?!

    Are there any caveats I need to be aware of with document.write that could be causing the problem?

     
    osu9400, Mar 17, 2008 IP
  6. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Not that I can think of. Ideally it would be better if you could dynamically insert the image on the server side, instead of with Javascript, but if that's not possible then what you have should work fine (as long as the visitor has Javascript enabled).
     
    vpguy, Mar 17, 2008 IP
  7. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #7
    Doing that can lead to bad timing. The .width property isn't available until the image file loads, and therefore should not be read immediately after assigning .src. An onload handler should be used:
    
    var i = new Image();
    i.onload=function(){document.images['image13'].width=Math.min(this.width, 680)}
    i.src = "../images/(1013).jpg";
    
    ......
    
    document.write("<a name='[B]image[/B]13' href='../index.htm#13'><img border='0' src='../images/(1013).jpg'  alt='(1013)'><br>");
    
    Code (markup):
     
    Logic Ali, Mar 18, 2008 IP
  8. osu9400

    osu9400 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Logic-

    Thanks for the suggestion. I implemented but it is still not resizing. Here is the resultant code. Am I missing something?

    
    <script type="text/javascript"><!--
    var i = new Image();
    i.onload=function(){document.images['Image14'].width=Math.min(this.width, 680)}
    i.src = "../images/(1014).jpg";
    
    document.write("<a name='Image14' href='../index.htm#14'><img border=0 src="+ i.src +"  alt='(1014)'><br>");
    //-->
    </script>
    [CODE]
    
    
    
    [quote="Logic Ali, post: 7017577"]Doing that can lead to bad timing. The .width property isn't available until the image file loads, and therefore should not be read immediately after assigning .src. An onload handler should be used:[CODE]
    var i = new Image();
    i.onload=function(){document.images['image13'].width=Math.min(this.width, 680)}
    i.src = "../images/(1013).jpg";
    
    ......
    
    document.write("<a name='[B]image[/B]13' href='../index.htm#13'><img border='0' src='../images/(1013).jpg'  alt='(1013)'><br>");
    
    Code (markup):
    [/QUOTE]
     
    osu9400, Mar 18, 2008 IP
  9. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #9
     
    Logic Ali, Mar 18, 2008 IP
  10. osu9400

    osu9400 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Because I don't know PHP. If it is simnpler than JS please let me know. I still can't get this JS onload stuff to work! When I place the doc.write first, the page loads with no image.

    I'm very frustrated.


     
    osu9400, Mar 18, 2008 IP
  11. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    What's the URL of the page which has the javascript code that writes the image (I think the URL above has the hard-coded version)? I can probably get it working. If you don't have it online yet, just name it something like test123.html and post the link here or PM me with it.
     
    vpguy, Mar 18, 2008 IP
  12. osu9400

    osu9400 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    vpguy-

    Here is the source. All of the %ABC% items are PhotoShop macros that get populated when I build the gallery. http://www.gentryfoto.com/test/SubPage.txt

    Here is one of the sample outputs:
    http://www.gentryfoto.com/test/pages/(1008)test.htm

    The original problem still exists. The page loads but the image does not. The image will load when I refresh the page. So, make sure your cache is clear.

    Thanks for your offer to help!


     
    osu9400, Mar 19, 2008 IP
  13. osu9400

    osu9400 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Here is the code to dynamically size the width of the image. Now, how can I add the ALT and HREF info to the image?!?!?

    
    <div id="photocontainer"></div>
    <script type="text/javascript">
    i=new Image();
    i.onload=function(){
    var scaleRatio=i.height/i.width;
    var w=Math.min(i.width, 680);
    var h=w*scaleRatio;
    i.height=h;
    i.width=w;
    var e=document.getElementById("photocontainer");
    if(!e)return;
    while(e.firstChild)e.removeChild(e.firstChild) //clear contents
    e.appendChild(i);
    }
    i.src="%IMAGESRC%";
    i.onclick
    </script>
    
    Code (markup):
     
    osu9400, Mar 24, 2008 IP