Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the raw JavaScript equivalent to jQuery's $(window).width

Tags:

jquery

width

I've heard it's offset.width, document.documentElement.clientWidth, and window.innerWidth

I'm curious for projects which I cannot use jQuery on, which solution I should use?

like image 761
hybrid Avatar asked Nov 22 '11 02:11

hybrid


2 Answers

function windowWidth() {
    var docElemProp = window.document.documentElement.clientWidth,
        body = window.document.body;
    return window.document.compatMode === "CSS1Compat" && docElemProp || body && body.clientWidth || docElemProp;
}

Taken (and modified slightly from the jQuery source:

https://github.com/jquery/jquery/blob/master/src/dimensions.js#L42

like image 120
Alex Peattie Avatar answered Oct 24 '22 03:10

Alex Peattie


See for yourself. It uses different things. document.documentElement.clientWidth is among them.

It can also use document.body.clientWidth

like image 44
DrStrangeLove Avatar answered Oct 24 '22 04:10

DrStrangeLove