0

I'm Trying to Figure out how to set column widths and heights using javascript so that i can set them according to the browser window. i figured the following code. but its not working. please help

<script type="javascript">
function size()
{
                    document.getElementById("body").style.width=window.innerWidth;
document.getElementById("body").style.height=window.innerHeight;
var width=document.getElementById("body").style.width;
var height=document.getElementById("body").style.height;

document.getElementById("header").style.height=(.15*height);
document.getElementById("loginbox").style.height=(.15*height);
document.getElementById("navigation").style.height=(.05*height);
document.getElementById("leftpanel").style.height=(.70*height);
document.getElementById("contentarea").style.height=(.75*height);
document.getElementById("addcolumn").style.height=(.75*height);
document.getElementById("footer").style.height=(.10*height);

document.getElementById("header").style.width=(.75*width);
document.getElementById("loginbox").style.width=(.25*width);
document.getElementById("navigation").style.width=(width);
document.getElementById("leftpanel").style.width=(.20*width);
document.getElementById("contentare").style.width=(.65*width);
document.getElementById("addcolumn").style.width=(.15*width);
document.getElementById("footer").style.width=(width);
}    
</script>

i use css to float the columns according to requirement. and then i'm calling the function size() in body ( onload=size() )

but not working

3
  • and you 100% sure that function running ? Commented Jul 20, 2013 at 9:52
  • So you have an element which ID is body? Seems strange, maybe you wanted to refer to document.body? Commented Jul 20, 2013 at 10:08
  • i removed the ' type=javascript ' and added a document.write("hello"). it was printed Commented Jul 21, 2013 at 17:46

3 Answers 3

2

style attributes are strings and need a unit in the end:

document.getElementById("addcolumn").style.width=(width + "px");

Chances are you've also need to set width and height as:

var width = window.innerWidth;
var height = window.innerHeight
Sign up to request clarification or add additional context in comments.

1 Comment

I've updated the answer. If you were to post a link to the page, or a fiddle, it would be a lot easier for people to help you.
0

I would suggest a different way to accomplish this, which will be easily mantainable:

function size() {
    var props = {
        header: {
            width: .75,
            height: .15
        },
        loginbox: {
            width: .25,
            height: .15
        },
        navigation: {
            width: 1,
            height: .05
        },
        leftpanel: {
            width: .2,
            height: .7
        },
        contentarea: {
            width: .65,
            height: .75
        },
        addcolumn: {
            width: .15,
            height: .75
        },
        footer: {
            width: 1,
            height: .1
        }
    },
        bodyElement = document.getElementById('body'),
        width = bodyElement.style.width = window.innerWidth,
        height = bodyElement.style.height = window.innerHeight;

    for (var id in props) {
        if (!props.hasOwnProperty(id)) continue;
        var element = document.getElementById(id),
            prop = props[id];
        element.width = (width * prop.width) + "px";
        element.height = (height * prop.height) + "px";
    }
}

Note: I haven't tested it, but it should work.

4 Comments

can u explain mein the condition in the for loop. i dont knw what it means
have you defined a array and are using a foreach loop?
i tried using it. but it didnt work. is there any way i can send u the code m typing?
Not without showing it to everyone. There is jsfiddle.net if you need to make a fiddle(or jsbin). Are you calling the function size() correctly?
0

Why not use simple css, make the width a percentage,

width: 75%;

3 Comments

width 75% of what? i have to take some value so that its percentage is defined. and that is why i'm trying to take the value as the brower width
if the div is a child of something, its width will be according to its parent, so if the parents width is the size of the browser, then it would work, you didn't post your html
yes. thats my ques. how to set the parent width equal to the size of the browser!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.