[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Resize body depending on browser width

Posted on 2006-11-09
6
Medium Priority
?
237 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
[X]
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
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
JohnModig earned 2000 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 29

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 29

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

649 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