Solved

Resize a popup window to image size

Posted on 2004-04-19
6
2,953 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

695 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