Solved

how to resize window to content

Posted on 2009-05-09
20
1,094 Views
Last Modified: 2013-11-19
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
Comment
Question by:PhotoCompManager
  • 11
  • 8
20 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24347709

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

no px
0
 

Author Comment

by:PhotoCompManager
ID: 24347748
No - still zero height (just the explorer toolbar)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24347761
Please remove the .style

it is document.body.clientHeight
0
 

Author Comment

by:PhotoCompManager
ID: 24347862
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
 
LVL 14

Expert Comment

by:ziffgone
ID: 24349115
Try "screen.availHeight".
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24349540
or just screen.height
0
 

Author Comment

by:PhotoCompManager
ID: 24352343
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
 

Author Comment

by:PhotoCompManager
ID: 24352350
Probably impossible
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24352368
Not impossible

Sorry to not have paid enough attention.

You want the document.body.scrollHeight
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24352383
And you need to do it AFTER you resize to 1020


0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24352392
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
 

Author Comment

by:PhotoCompManager
ID: 24352535
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24352756
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
 

Author Comment

by:PhotoCompManager
ID: 24353166
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24353624
Yeah, that does not do it. Sorry.

It is quite hard to get the chrome and then apply it.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 24353740
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
 

Author Comment

by:PhotoCompManager
ID: 24355581
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24357047
Perhaps just accept my suggestion then?
0
 

Author Closing Comment

by:PhotoCompManager
ID: 31579787
Sorry - thought I had accepted it
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24361912
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

895 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

17 Experts available now in Live!

Get 1:1 Help Now