Solved

how to resize window to content

Posted on 2009-05-09
20
1,093 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
Comment Utility

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

no px
0
 

Author Comment

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

Expert Comment

by:Michel Plungjan
Comment Utility
Please remove the .style

it is document.body.clientHeight
0
 

Author Comment

by:PhotoCompManager
Comment Utility
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
Comment Utility
Try "screen.availHeight".
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
or just screen.height
0
 

Author Comment

by:PhotoCompManager
Comment Utility
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
Comment Utility
Probably impossible
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Not impossible

Sorry to not have paid enough attention.

You want the document.body.scrollHeight
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
And you need to do it AFTER you resize to 1020


0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perhaps just accept my suggestion then?
0
 

Author Closing Comment

by:PhotoCompManager
Comment Utility
Sorry - thought I had accepted it
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

771 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

10 Experts available now in Live!

Get 1:1 Help Now