Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic height of popup 2

Status
Not open for further replies.

Masali

Programmer
Jun 19, 2002
143
SE
I have a popup that loads text from a database. The text is different in length.

I want a javascript that makes the window the same size that the text takes in height. So the window does not display any scrollerbars and all the text is fully visible in the window and the window is not bigger than the text.

Please advise

Masali
 

As far as I can tell, the only way to do this would be if you could guarantee the font the user had on their system... And then it woul only work if the font was displayed in an absolute size (pixels) and their browser could never change that.

So... assuming all of that, you need to build a table of widths and heights for each character, know where your line wraps are, and work out the dimensions.

And yes - it can be done... I've used clipping tables for Arial in several sizes for work on corporate intranets... Time consuming, but needed at the time.

At the end of the day, you've got to wonder whether it's a lot of work just to try and eliminate scrollbars. Add to that the fact that the text might grow bigger than the size of the users screen, and then you have the dilemma of how they see it without scrollbars.

Hope this is food for thought,

Dan
 
I loaded the page in the popup window and put the text for the page into a div. I then got the height and width of the div and resized the window. I found that I had to do this a second time to make it work properly.

First off... copy the following and save it as start.html:

Code:
<html>
<head>
<script type=&quot;text/javascript&quot;>
function openPopup(_width)
{
  var myWin = window.open(&quot;mypopup.html&quot;,&quot;mywin&quot;,&quot;top=50,left=50,width=&quot;+_width+&quot;,height=100&quot;);
}
</script>
</head>

<body>
<a href=&quot;javascript:openPopup('100');&quot;>Open Popup at width = 100</a><br>
<a href=&quot;javascript:openPopup('200');&quot;>Open Popup at width = 200</a><br>
<a href=&quot;javascript:openPopup('300');&quot;>Open Popup at width = 300</a><br>
<a href=&quot;javascript:openPopup('400');&quot;>Open Popup at width = 400</a>
</body>
</html>[code]

Now save the following as [b]mypopup.html[/b]:

[code]<html>
<head>
<title>Tek Tips Rock</title>
<script type=&quot;text/javascript&quot;>
function resizeMe()
{
  var myHeight = document.getElementById(&quot;mycontent&quot;).offsetHeight + 35;
  var myWidth = document.getElementById(&quot;mycontent&quot;).offsetWidth;
  resizeTo(myWidth,myHeight);

  myHeight = document.getElementById(&quot;mycontent&quot;).offsetHeight + 35;
  myWidth = document.getElementById(&quot;mycontent&quot;).offsetWidth;
  resizeTo(myWidth,myHeight);
}
</script>
</head>

<body style=&quot;padding:5px;margin:5px&quot; onload=&quot;resizeMe()&quot;>
<div id=&quot;mycontent&quot;>
<p>Sed nec dui. Phasellus condimentum augue non lorem. Pellentesque vitae ante. Vestibulum feugiat blandit risus. Curabitur cursus, velit ac faucibus volutpat, massa turpis posuere mi, nec condimentum sem metus id magna. Duis in justo. Cras sit amet pede non magna fermentum vestibulum. In nisl orci, euismod non, malesuada quis, placerat id, lorem. Vivamus vitae augue. Donec venenatis metus vel urna.</p><p>Vivamus tortor. Sed est. Cras pede. Nullam mollis lectus quis nisl. Praesent quis urna. Morbi sollicitudin velit sit amet tortor. Pellentesque imperdiet nulla et urna rutrum tincidunt.</p><p>Vivamus euismod augue nec magna. Morbi vitae elit sed justo eleifend porta.</p>
</div>
</body>
</html>

Open up start.html in your browser (I used IE 5.17 for Mac for this). Now click one of the links on the page... each one opens the popup at a different width, and resizes itself so that you have no scrollbars.

I've not extensively tested this across all browsers... see how it works for you.

Jeff
 

Good solution, Jeff... Looks like my font tables are now obsoleted (lucky I only used them for IE4 and NN4 ;o)

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top