SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    CA
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Autosizing popup to image size?

    Hi -

    I'm putting together an image gallery on my site, and have a javascript that is "supposed" to allow the visitor to click the thumbnail of an image and have it pop up in a new window that is automatically sized to the width and height (plus padding) of the image. It seems to work... kinda. Problem is, the first time a thumbnail is clicked, it doesn't size the window correctly. Clicking it after that it seems to work fine for the most part. Anyhow, I'm hoping someone can help me get this code working properly...

    You can see it in action at:
    http://www.absolutecross.com/graphics/gallery/

    Here's the code:
    Code:
    <SCRIPT LANGUAGE="Javascript">
    <!-- Begin auto-sizing window script
    function image_open(image_loc)
    {
     var var_image = new Image();
     var_image.src = image_loc;
     width = var_image.width + 20;
     height = var_image.height +30;
     str_style= "\" width="+width+",height="+height+"\"";
     window.open(var_image.src,"myImage",str_style);
     
    }
    //-- End auto-sizing window script -->
    </SCRIPT>
    
    <IMG SRC="http://www.absolutecross.com/images/gallery/thumb_ac-ancient.jpg"
     HSPACE="0" VSPACE="0" BORDER="0" WIDTH="60" HEIGHT="60" BORDER="0" ALT="AC Ancient Panel"
     onclick="image_open('http://www.absolutecross.com/images/gallery/ac-ancient.jpg')">
    Also it seems to add an immense amount of extra width to the window in Netscape 6, and I haven't been able to test it in NS4 and other browsers besides IE6 as I just finished building this computer and haven't located where to download older versions yet hehe...

    Thanks!

    *edit: fixed img code so it wouldnt screw up forum layout
    Last edited by AC Design; Jan 10, 2002 at 03:19.

  2. #2
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Autosizing popup to image size?

    Hey, nice site!!

    Originally posted by AC Design

    *edit: fixed img code so it wouldnt screw up forum layout
    Lol. No you didn't!

    Anyway, back to the topic:

    Also it seems to add an immense amount of extra width to the window
    This is possibly something to do with this:

    Code:
    width = var_image.width + 20;
     height = var_image.height +30;
    and the way in which NS renders sizes.

    As for the actual script, then this has been discussed a lot... it is possible (try doing a search and it might bring something up) and is certainly possible with PHP, but whilst I find the script for you (I know it is available on www.javascriptsource.com) then it is possible to do with the fact that you reference the name of the image (myImage) in the code, but you don't in the image opening tag. You could try messing around with that to see if it helps.

    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is that the Image has to load before its width/height is known, other wise it returns 0, and the popup windows defaults are used, which is 100/100 (unlike in typical JS the function doesn't wait for the Image to load(cache) before moving on).

    I tried looping around till the image w/h aren't 0, but didn't have success with NS4/others. This is a very difficult script to write, although I got it to work fine with IE.

    Good Luck


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
  •