+ Reply to Thread
Results 1 to 8 of 8

Thread: javascript help - window width

  1. #1
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    javascript help - window width

    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.

  2. #2
    Jake's Avatar
    Jake is offline Developer Jake is an unknown quantity at this point
    Join Date
    Apr 2005
    Location
    Winona, MN
    Posts
    2,084

    Re: javascript help - window width

    I think you are looking for something like this?
    http://www.howtocreate.co.uk/tutoria.../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 by Jake; 10-29-2008 at 03:52 PM.
    Jake Omann | Developer
    █ 888-X10-9668 - jake[@]x10hosting.com
    x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  3. #3
    xmakina's Avatar
    xmakina is offline x10 Lieutenant xmakina is an unknown quantity at this point
    Join Date
    May 2008
    Location
    England
    Posts
    265

    Re: javascript help - window width

    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 Code:
    <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>
    IF($this->$post.content() == "SEE SIG"){
    w3Schools and Google
    }

  4. #4
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    Re: javascript help - window width

    thank you, i will try these. i want a div that is centered regardless of browser size.

  5. #5
    xmakina's Avatar
    xmakina is offline x10 Lieutenant xmakina is an unknown quantity at this point
    Join Date
    May 2008
    Location
    England
    Posts
    265

    Re: javascript help - window width

    Just use
    Code:
    padding:auto;
    IF($this->$post.content() == "SEE SIG"){
    w3Schools and Google
    }

  6. #6
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    Re: javascript help - window width

    i've never heard of using auto in padding. does it center it?
    Edit:
    jake, your code worked. thank you.
    Last edited by kbjradmin; 10-30-2008 at 12:17 PM. Reason: Automerged Doublepost

  7. #7
    xmakina's Avatar
    xmakina is offline x10 Lieutenant xmakina is an unknown quantity at this point
    Join Date
    May 2008
    Location
    England
    Posts
    265

    Re: javascript help - window width

    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
    IF($this->$post.content() == "SEE SIG"){
    w3Schools and Google
    }

  8. #8
    Jake's Avatar
    Jake is offline Developer Jake is an unknown quantity at this point
    Join Date
    Apr 2005
    Location
    Winona, MN
    Posts
    2,084

    Re: javascript help - window width

    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.
    Jake Omann | Developer
    █ 888-X10-9668 - jake[@]x10hosting.com
    x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

+ Reply to Thread

Similar Threads

  1. 300 short cuts of Windows XP
    By goodone in forum Computers & Technology
    Replies: 34
    Last Post: 11-29-2011, 12:12 AM
  2. drop down menus with JavaScript disabled?
    By sifaka in forum Free Hosting
    Replies: 1
    Last Post: 05-15-2008, 10:46 AM
  3. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  4. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM
  5. Some nice-simple JavaScript effects !!
    By careerbridge in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 07-13-2006, 08:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers