Link to home
Start Free TrialLog in
Avatar of PhotoCompManager
PhotoCompManager

asked on

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?
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image


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

no px
Avatar of PhotoCompManager
PhotoCompManager

ASKER

No - still zero height (just the explorer toolbar)
Please remove the .style

it is document.body.clientHeight
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?
Try "screen.availHeight".
or just screen.height
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
Probably impossible
Not impossible

Sorry to not have paid enough attention.

You want the document.body.scrollHeight
And you need to do it AFTER you resize to 1020


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
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.
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

Yeah, that does not do it. Sorry.

It is quite hard to get the chrome and then apply it.
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Perhaps just accept my suggestion then?
Sorry - thought I had accepted it
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