Resize body depending on browser width

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
LearningCSharpSQLAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
JohnModigConnect With a Mentor Commented:
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
 
Pravin AsarPrincipal Systems EngineerCommented:
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
 
LearningCSharpSQLAuthor Commented:
Hi Sorry
My mistake i'm trying to apply the code for the body tag, not the<div id="divMainContent

Regards
George
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JohnModigCommented:
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
 
Pravin AsarPrincipal Systems EngineerCommented:
<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
 
LearningCSharpSQLAuthor Commented:
Sorry for delay, big problems here Thanks
0
All Courses

From novice to tech pro — start learning today.