Solved

Resize body depending on browser width

Posted on 2006-11-09
6
229 Views
Last Modified: 2012-06-27
Hi
Can anyone help with this section of code i found:

function resizeDiv()
{
   var newWidth=document.body.clientWidth-100;
   if (newWidth>1000) newWidth=1000;
   document.getElementById("divMainContent").style.width=newWidth;
}
window.onresize=resizeDiv;
</script>
</head>
<body onload="resizeDiv()">
<div id="divMainContent" style="border: 1px solid black; width:100px;">test</div>
</body>

What I want is to resize the body depending on the browser size, i.e if browser is 1024 allow the body to expand to 1024 or shrink, if the body is more than 1024, do not allow the body to expand any wider than that 1024, but still allow it to contract. Code must work in all major browsers.

Any help will be appreciated.

Regards
George
0
Comment
Question by:LearningCSharpSQL
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
JohnModig earned 500 total points
ID: 17909600
Hi George.
Please try the following code:

--------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Javascript test</title>
<style type="text/css" title="currentStyle" media="all">
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
body {
font: small verdana;
background: white;
}
#divMainContent {
background: red;
height: 100%;
}
</style>
<script type="text/JavaScript">
//<![CDATA[
function resizeDiv(){
if (document.body.clientWidth>1024){
document.getElementById("divMainContent").style.width='1024px';
}
else {document.getElementById("divMainContent").style.width='auto';}
}
window.onresize=resizeDiv;
//]]>
</script>
</head>
<body>
<div id="divMainContent">
All content goes here.
</div>
</body>
</html>
--------------------------------------

Note:
This is basicly a workaround for the CSS attribute "maxwidth". Maxwidth is not supported by IE, therefore this javascript might do the trick better.

Regards,
John
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 17909647
Here is a complete solution,

This is cross-browser solution
With this you the all window inquiry functions..enjoy.


<html>
<head>
<title>Window and Screen Size Details</title>
</head>
<body onload="resizeDiv()">
<script language="javascript">

var wLeft = 0;
var wTop  = 0;
var wWidth=800;
var wHeight=600;
var scrWidth=0;
var scrHeight=0;


function GetWindowDetails () {
      WindowSize();
      GetWindowLocation();
      GetScreenResolution();
      this.wWidth = wWidth;
      this.wHeight = wHeight;
      this.sWidth = scrWidth;
      this.sHeight = scrHeight;
      this.wLeft = wLeft;
      this.wTop  = wTop;
      return this;
}
// Get Window Size
function WindowSize()
{
     if( typeof( window.innerWidth ) == 'number' ) {
          //Non-IE
          wWidth = window.innerWidth;
          wHeight = window.innerHeight;
     }
     else if( document.documentElement &&
              (document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ) {
          //IE 6+ in 'standards compliant mode'
          wWidth = document.documentElement.clientWidth;
          wHeight = document.documentElement.clientHeight;
     }
     else if( document.body && 
              (document.body.clientWidth ||
                 document.body.clientHeight ) ) {
          //IE 4 compatible
          wWidth = document.body.clientWidth;
          wHeight = document.body.clientHeight;
     }
}

// Get Window Height
function GetWindowHeight () {
    WindowSize();
     return (wHeight-20);
}

// Get Window Width
function GetWindowWidth () {
    WindowSize();
     return (wWidth);
}

// Get Screen Resolution

function GetScreenResolution () {
   scrWidth = window.screen.width;
   scrHeight = window.screen.height;
}

// Get Window Location
function GetWindowLocation () {
    if (!document.layers && document.all) {
          wLeft = window.screenLeft;
          wTop = window.screenTop;
    }
    else {
          wLeft = window.screenX;
          wTop  = window.screenY;
    }
}

function resizeDiv()
{
   var wndDetail = new GetWindowDetails();
   var newWidth=wndDetail.wWidth - 100;
   if (newWidth>1000) newWidth=1000;
   document.getElementById("divMainContent").style.width=newWidth;
}
window.onresize=resizeDiv;
</script>
</head>
<div id="divMainContent" style="border: 1px solid black; width:100px;">test</div>


</body>
</html>
0
 

Author Comment

by:LearningCSharpSQL
ID: 17909848
Hi Sorry
My mistake i'm trying to apply the code for the body tag, not the<div id="divMainContent

Regards
George
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 11

Expert Comment

by:JohnModig
ID: 17909944
The body is the browsers view port and it takes up 100% of the windows width (minus borders). So you can not change the body witdh - if that was the case, then what would there be instead to fill up the window?

Suggestions:

1. Simply put all your content in the <div id="divMainContent">.

2. Or do you mean you rather want to resize then window itself?
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 17909962
<html>
<head>
<title>Window and Screen Size Details</title>
</head>
<body style="border: solid; width: 500;height: 500px;" onload="resizeDiv()">
<script language="javascript">

var wLeft = 0;
var wTop  = 0;
var wWidth=800;
var wHeight=600;
var scrWidth=0;
var scrHeight=0;


function GetWindowDetails () {
      WindowSize();
      GetWindowLocation();
      GetScreenResolution();
      this.wWidth = wWidth;
      this.wHeight = wHeight;
      this.sWidth = scrWidth;
      this.sHeight = scrHeight;
      this.wLeft = wLeft;
      this.wTop  = wTop;
      return this;
}
// Get Window Size
function WindowSize()
{
     if( typeof( window.innerWidth ) == 'number' ) {
          //Non-IE
          wWidth = window.innerWidth;
          wHeight = window.innerHeight;
     }
     else if( document.documentElement &&
              (document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ) {
          //IE 6+ in 'standards compliant mode'
          wWidth = document.documentElement.clientWidth;
          wHeight = document.documentElement.clientHeight;
     }
     else if( document.body && 
              (document.body.clientWidth ||
                 document.body.clientHeight ) ) {
          //IE 4 compatible
          wWidth = document.body.clientWidth;
          wHeight = document.body.clientHeight;
     }
}

// Get Window Height
function GetWindowHeight () {
    WindowSize();
     return (wHeight-20);
}

// Get Window Width
function GetWindowWidth () {
    WindowSize();
     return (wWidth);
}

// Get Screen Resolution

function GetScreenResolution () {
   scrWidth = window.screen.width;
   scrHeight = window.screen.height;
}

// Get Window Location
function GetWindowLocation () {
    if (!document.layers && document.all) {
          wLeft = window.screenLeft;
          wTop = window.screenTop;
    }
    else {
          wLeft = window.screenX;
          wTop  = window.screenY;
    }
}

function resizeDiv()
{
   var wndDetail = new GetWindowDetails();
   var newWidth=wndDetail.wWidth - 100;
   if (newWidth>1000) newWidth=1000;
   document.body.style.width=newWidth;
   document.body.style.height=wndDetail.wHeight-100;
}
window.onresize=resizeDiv;
</script>
</head>
<div id="divMainContent" style="border: 1px solid black; width:100px;">test</div>


</body>
</html>
0
 

Author Comment

by:LearningCSharpSQL
ID: 17943712
Sorry for delay, big problems here Thanks
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

803 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