Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Resize body depending on browser width

Posted on 2006-11-09
6
230 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
removing a class in javascript 4 47
Override MS CSS in SharePoint Online Modal Form 19 40
html border input line 7 13
Do alert on select 6 13
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

809 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