Solved

Resize a popup window to image size

Posted on 2004-04-19
6
2,944 Views
Last Modified: 2007-12-19
Hi - I've got some code that loads a popup window with a pic and text underneath it.  This is not a separate html file and was coded this way to avoid hundreds of extra html files.  The enlarge works fine except for one detail - the image height and widths get cached.  Check for yourselves here (click the opaque thumbs at the bottom):

http://www.greenteadesign.ca/reproductions/elmburltest.html

Here is the code:

<script>
      function openWin(url,descr){
        var obj=document.images["tempImg"],txt=document.getElementById("tempDiv");
        obj.src=url;
        obj.style.display="inline";
        var margin=1;
        var w=obj.offsetWidth+2*margin,h=obj.offsetHeight+margin*40;
        obj.style.display="none";
        txt.style.width=w-margin*2;
        txt.innerHTML=descr;
        txt.style.display="inline";
        h+=txt.offsetHeight;
        txt.style.display="none";
        var win=window.open("","win","width="+w+",height="+h+",left=0,top=0,menubar=no,location=no,status=no,directories=no,resizable=yes");
        win.document.write("<img style='border: solid 1px #000' src='"+url+"'><p>"+descr);
        win.document.body.style.margin=margin;
        win.document.body.style.textAlign="center";
        win.document.body.style.fontFamily="Arial";
        win.document.body.style.fontWeight="Bold";
        win.document.body.style.fontSize="14px";
      }
</script>

Links look like this:
<a href="javascript:openWin('/images/reproductions/step_tansu/enlarge-elmburl.jpg','Elm Burl with Golden Pecan stain and Brass Hirute hardware.');">

And the hidden divs:

<img name=tempImg style="display:none;">
<div id=tempDiv style="display:none;">&nbsp;</div>

I need a way to resize the window to the correct size.  When I was using a separate html file for each enlarge I had the following function onload:
function goto() {
self.resizeTo(document.images[0].width+15, document.images[0].height+62)
}

I don't know JS and have been fooling around with win.resizeTo but couldn't figure it out.

Any help would be greatly appreciated,
Hayden

 
0
Comment
Question by:Quarfelburg
6 Comments
 
LVL 10

Expert Comment

by:Shailesh15
Comment Utility
If you know width & height before opening the window...

       win.document.body.style.fontWeight="Bold";
       win.document.body.style.fontSize="14px";
       win.resizeTo(w,h);// new line
     }
</script>


On load document use...

window.resizeTo(document.images[0].width+15, document.images[0].height+62)
0
 

Author Comment

by:Quarfelburg
Comment Utility
It would be a pain to have to specify width and height of image for hundreds of enlarges.  

How can I do an onload if I am not using a separate html file?
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 125 total points
Comment Utility
You can insert the resize code like this:

<script>
     function openWin(url,descr){
       var obj=document.images["tempImg"],txt=document.getElementById("tempDiv");
       obj.src=url;
       obj.style.display="inline";
       var margin=1;
       var w=obj.offsetWidth+2*margin,h=obj.offsetHeight+margin*40;
       obj.style.display="none";
       txt.style.width=w-margin*2;
       txt.innerHTML=descr;
       txt.style.display="inline";
       h+=txt.offsetHeight;
       txt.style.display="none";
       var win=window.open("","win","width="+w+",height="+h+",left=0,top=0,menubar=no,location=no,status=no,directories=no,resizable=yes");
       win.document.write("<img style='border: solid 1px #000' src='"+url+"'><p>"+descr);
       win.document.body.style.margin=margin;
       win.document.body.style.textAlign="center";
       win.document.body.style.fontFamily="Arial";
       win.document.body.style.fontWeight="Bold";
       win.document.body.style.fontSize="14px";
       newImg = new Image();
       newImg.src = url;
       win.resizeTo(newImg.width+15, newImg.height+62)
     }
</script>


Good luck,
Zvonko

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:HaydnH
Comment Utility
FYI: There appear to be a few problems with your css as they look different in different browsers (namely IE and Mozilla) - if you would like screenies let me know - otherwise download Mozilla to see what I mean.

Haydn.
0
 

Author Comment

by:Quarfelburg
Comment Utility
Thanks Zvonko, works great.  HaydenH thanks I will fix it ASAP (client told me not to worry about anything other than ie5 - finally convinced him otherwise).

Hayden.
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Fine to see it life working for you :-)
Thanks for the points.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now