1

I'm trying to read the width of a div without jQuery. The code I'm using is:

JS:

var windowwidth = parseInt(document.getElementById("window").style.width);
window.onload = function start() {
    alert(windowwidth);
}

HTML:

<div id="window">
    <p>Lorem ipsum dolor sit amet.</p>
</div>

CSS:

#window {
    height: 250px;
    margin: 0 auto;
    width: 600px;
}

I don't understand why this isn't working, I've looked all over this site and google and can't figure out a solution. I don't want to use jQuery for this (it's a small custom HTML5 script so I don't need that huge library for one little thing) and even when I try the jQuery method, it still doesn't work.

2
  • 1
    Why not look in the jQuery source code to see how they did it? Commented Jun 25, 2011 at 19:51
  • Oo that might be a good idea if I can't figure it out another way, thanks. Commented Jun 25, 2011 at 20:02

5 Answers 5

3

You're not guaranteed that document.getElementById() will work (i.e. will return the required element) before the DOM is ready. So try:

window.onload = function() { // removed the name to avoid some IE leaks
    var windowwidth = parseInt(document.getElementById("window").style.width);
    alert(windowwidth);
}
4
  • That at least gave me the pop up, but it's giving me a value of NaN. Any ideas why? It works if I set the width inline, but I'd eventually like to release this for more than just me to use, and that might be a little confusing for people who don't read instructions. Commented Jun 25, 2011 at 19:59
  • @Rev, I genuinely recommend you include jQuery (or a micro framework that is much lighter) even if it's just for this one little thing. Chances are you'll use it in other places, but even if not, it's a small price to pay to not have to implement this yourself and test and continually support new browsers etc. Like you said, .style.width isn't the best solution, so you start using .offsetWidth which isn't standard, and the spaghetti begins...
    – davin
    Commented Jun 25, 2011 at 20:06
  • Although you can try using document.getElementById("window").offsetWidth, not sure how it works with scroll and other such things (that a library would take care of), and like I said, it's not standard (although AFAIK widely supported)
    – davin
    Commented Jun 25, 2011 at 20:09
  • 1
    This shouldn't work and if it does, then the code posted is incorrect. You would have to use getComputedStyle and currentStyle with a combination of offsetWidth to get this to work properly.
    – Lime
    Commented Jun 25, 2011 at 20:27
2

don't read the style property, read the actual width of the element:

window.onload = function(){
    alert(document.getElementById('window').offsetWidth);
}

offsetWidth is what the browser says is the width, which can be different than what you're setting it to with CSS if, for example, the content stretches it wider.

1

If you want to see how jQuery does it, here is the link to the source: https://github.com/jquery/jquery/blob/master/src/dimensions.js

0

I would use jQuery to do such a thing. You can find the exact thing you want right here: http://api.jquery.com/width/

0

You can get style.width only after the element is drawn. Try to put your code to setTimeout(). Sometimes it helps me

window.onload = function(){
    setTimeout(
       function(){ alert(document.getElementById('window').style.width); },
       200
    );
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.