• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1149
  • Last Modified:

how to resize window to content

I'm trying to resize the browser window so that it just fits the content, or has scroll bars if the content is longer than the screen height. (I create the content dynamically in ASPX)
I've put the following in a function and called it on body onload

        window.resizeTo(1020,document.body.style.clientHeight+'px');

but I get zero height. I've also tried with.Height and with a Div that encompasses the whole body, but still get zero height.

Can you suggest as javascript or CSS solution?
0
PhotoCompManager
Asked:
PhotoCompManager
  • 11
  • 8
1 Solution
 
Michel PlungjanIT ExpertCommented:

        window.resizeTo(1020,document.body.style.clientHeight);

no px
0
 
PhotoCompManagerAuthor Commented:
No - still zero height (just the explorer toolbar)
0
 
Michel PlungjanIT ExpertCommented:
Please remove the .style

it is document.body.clientHeight
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
PhotoCompManagerAuthor Commented:
Well I now get some of the body, but not all. I need to allow for the space taken by the browser frame and menu area, but there doesn't seem to be a window.height and window.avaiable height

Any ideas?
0
 
ziffgoneCommented:
Try "screen.availHeight".
0
 
Michel PlungjanIT ExpertCommented:
or just screen.height
0
 
PhotoCompManagerAuthor Commented:
Sorry, but I need the window to fit the body. I've done more research into DHTML and I think it ia probably impossible. Non-IE browsers supply the window height but IE does not.
I'm giving up - thanks for your help
0
 
PhotoCompManagerAuthor Commented:
Probably impossible
0
 
Michel PlungjanIT ExpertCommented:
Not impossible

Sorry to not have paid enough attention.

You want the document.body.scrollHeight
0
 
Michel PlungjanIT ExpertCommented:
And you need to do it AFTER you resize to 1020


0
 
Michel PlungjanIT ExpertCommented:
Here is how in an iframe. You see the attributes involved whether or not you want to use an iframe

http://stackoverflow.com/questions/525992/resize-iframe-height-according-to-content-height-in-it
0
 
PhotoCompManagerAuthor Commented:
I'm totally confused.
I want to set the window height to the content height.
The Iframe example seems to set the content size ( iframe or body) to the window size.
I can't see any attributes on the window object in IE that lets me set the size apart from the resize method.

window.resizeTo(1020,document.body.scrollHeight); does not allow for the "chrome" - the menu, toolbars etc.
0
 
Michel PlungjanIT ExpertCommented:
The issue here is that you change the length of the content when you mess with the width.

try this:

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20917957.html#a10592811
0
 
PhotoCompManagerAuthor Commented:
Well, the idea looks good but it doesn't work because document.body.offsetHeight does not change when the window is resized.

I've attatched the referenced code with a few extra alerts.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
   function setPageSize()     
    {
if (document.all){
cW=document.body.offsetWidth;
cH=document.body.offsetHeight;
alert('docH ' + cH);
window.resizeTo(500,500);
alert('resized docH ' + document.body.offsetHeight);
alert('scroll docH ' + document.body.scrollHeight);
barsW=500-document.body.offsetWidth;
barsH=500-document.body.offsetHeight;
alert('barsH ' + barsH);
wW=barsW+cW;
wH=barsH+cH;
alert('resize to ' + wH);
window.resizeTo(wW,wH);
alert('resized ' + wH);
}
else
{
wW=window.outerWidth;
wH=window.outerHeight;
alert('else ' +wW + '  ' + wH);
}
}
</script>
</head>
<body onload="setPageSize()">
<p>Text</p><p>Text</p><p>Text</p><p>Text</p><p>Text</p><p>Text</p>
<p>Text</p><p>Text</p><p>Text</p><p>Text</p><p>Text</p><p>Text</p>
<p>Text</p><p>Text</p><p>Text</p><p>Text</p><p>Text</p><p>Text</p>
</body>
</html>

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
Yeah, that does not do it. Sorry.

It is quite hard to get the chrome and then apply it.
0
 
Michel PlungjanIT ExpertCommented:
Bingo

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23293013.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
<script>
function setPageSize(){
 
    var de = document.documentElement;
 
    var width = de.scrollWidth - de.clientWidth;
    var height = de.scrollHeight - de.clientHeight;
 
    window.resizeBy(width, height);
    window.moveTo((screen.width - de.clientWidth) / 2, (screen.height - de.clientHeight) / 2);
      
}
</script>
</head>
<body onload="setPageSize()">
<div id="content" style="background-color:teal; width:100%; height:100%">
<p>Text</p>
<p>Text</p>
<p>Text</p>
<p>Text</p>
<p>Text</p>
<p>Text</p>
<p>Text</p>
<p>Text</p>
<p>Text</p>
<p>Text</p>
<p>Text</p>
<p>Text</p>
<p>Text</p>
<p>Text</p>
</div>
</body>
</html>

Open in new window

0
 
PhotoCompManagerAuthor Commented:
Great!!!!!

I've added a tweak to make sure the window height never exceeds the screen height (but I haven't tested other browsers apart from IE).

Thanks for staying with it
function sizeWindowToContent(){
    var de = document.documentElement;
    var width = de.scrollWidth - de.clientWidth;
    var height = de.scrollHeight - de.clientHeight;
    window.moveTo((screen.availWidth) / 2, (screen.availHeight) / 2);
    if (navigator.appName =='Microsoft Internet Explorer') {
	var wTop=window.screenTop;
	var wLeft=window.screenLeft;
   } 
    else {
	var wTop=window.screenY;
	var wLeft=window.screenX;
    }	
    var vChrome=wTop-(screen.availHeight/2) +de.scrollHeight-document.body.offsetHeight;
    var hChrome=wLeft-(screen.availWidth/2) +de.scrollWidth-document.body.offsetWidth +8;
    if (de.scrollHeight +vChrome>screen.availHeight) resizeTo(de.scrollWidth+hChrome,screen.availHeight);else window.resizeBy(width, 
 
height);
    window.moveTo((screen.availWidth - (de.clientWidth+hChrome)) / 2, (screen.availHeight - (de.clientHeight+vChrome)) / 2);
}

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
Perhaps just accept my suggestion then?
0
 
PhotoCompManagerAuthor Commented:
Sorry - thought I had accepted it
0
 
Michel PlungjanIT ExpertCommented:
No problem, you had in fact accepted your own solution with mine as assisted solution - that starts a process in community support where a moderator is needed to finalise.

Michel
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 11
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now