Solved

Resize a popup window to image size

Posted on 2004-04-19
6
2,949 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
ID: 10863182
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
ID: 10863289
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
ID: 10864297
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 8

Expert Comment

by:HaydnH
ID: 10867103
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
ID: 10870252
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
ID: 10870352
Fine to see it life working for you :-)
Thanks for the points.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

813 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

18 Experts available now in Live!

Get 1:1 Help Now