Solved

Resize body depending on browser width

Posted on 2006-11-09
6
226 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

744 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

15 Experts available now in Live!

Get 1:1 Help Now