PDA

View Full Version : content positioning w/ small javascript



nervegas87
Mar 25th, 2007, 07:53 PM
i made this javascript using the new "screen.width" property, which would make it easy to center content in a webpage no matter what the screen resolution is. Here it goes:



<html>
<head>
<script type="text/javascript">
function pos_center() {
var content = document.getElementById("carrier")
var width = screen.width
var contentwidth = content.offsetWidth
value = parseFloat((width - contentwidth)/2)
content.style.marginLeft = value
}
</script>
</head>
<body onload="pos_center()">
<div class="body" id="carrier">
</div>
</body>
</html>


The "body onload" makes the page first load all the way to the left, and then after completely loading switches the content to the center, that is dynamically gross.
Now, instead of using the "body onload", is there any other way that i can make the content appear. Seeing that there is no "onbeforeload", there must be another way to solve this.

1212jtraceur
Mar 25th, 2007, 09:43 PM
You could use CSS...Otherwise, I do not know of any other event that would work for you. I do have one suggestion, though: use



document.defaultView.getComputedStyle(document.body, '').getPropertyValue('width')


instead of
screen.width

This is because screen.width is the width of the screen and not the width of the content area of the user agent.