javascript help - window width

kbjradmin

New Member
Messages
512
Reaction score
2
Points
0
i need a script that will determine the inner width of a browser window. i tried this
Code:
winwidth=document.all?document.body.clientwidth:window.innerwidth;
padding = (winwidth-1000)/2;
document.write("<div style=\"position:absolute;top:25px;left:" + padding + "px;\">");
but it doesn't seem to work. it isn't reporting an error, but not working either.

if you can fix this code or give me a new one, i don't care which, just help.
 

Jake

Developer
Contributors
Messages
4,057
Reaction score
5
Points
0
I think you are looking for something like this?
http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

Code:
if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && document.documentElement.clientWidth ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }

should work,
also why are you doing this anyway?
 
Last edited:

xmakina

New Member
Messages
264
Reaction score
0
Points
0
If this is for a CSS Div might I recommend you do away with it? CSS can use percentages for width, you know, and then the browser will adjust if the window size is changed.

I'm guessing you want a box with some border around it, use
Code:
div#container{
	padding:25px;
	background-color:red;
}
div#header{
	background-color:green;
}
div#content{
	background-color:blue;
}
div#footer{
	background-color:yellow;
}
with
HTML:
<html>
	<head>
		<title>WriterSite</title>
		<link rel="stylesheet" type="text/css" href="style.css" />
	</head>
	
	<body>
		<div id="container">
			<div id="header">
				<p class="navigation">WriterSite</p>
				<h1>Welcome</h1>
			</div>
			<div id="content">
				<p>This website is for Omnisoft WriterSite</p>
			</div>
			<div id="footer">
				<p>Copyright (C) Me 2008</p>
			</div>
		</div>
	</body>
</html>
 

kbjradmin

New Member
Messages
512
Reaction score
2
Points
0
thank you, i will try these. i want a div that is centered regardless of browser size.
 

kbjradmin

New Member
Messages
512
Reaction score
2
Points
0
i've never heard of using auto in padding. does it center it?
Edit:
jake, your code worked. thank you.
 
Last edited:

xmakina

New Member
Messages
264
Reaction score
0
Points
0
yes - auto centres the div. I'm just thinking you should avoid JScript where possible as people like myself use NoScript a lot and it can really cause a site to look awful if the developer loves their JScript
 

Jake

Developer
Contributors
Messages
4,057
Reaction score
5
Points
0
It all depends, there are times when it is useful... the difference is that if you wanted to say make a popup using ajax that pops up in the middle of the screen this javascript is good for finding the center. However if its a normal website you can use css...

margin: auto; would center the whole thing in the middle

margin 10px auto; would center it width-wise while staying 10 pixels down from the top of the browser window.
 
Top