How to get the window width with JavaScript

Again, open the browser console quickly to figure something out. This time, I need the width and height of the current window for clublink.to:

trying to find the width

These words keep going through my mind (it goes through any developer's mind actually): This shouldn't take long. Of course, I can't figure it out in a few minutes. So I end up googling it. After a bit, I had stumbled upon a gist by Josh Carr, to keep it for myself from searching again for the solution, I'll note it here for myself:

// vanilla JS window width and height
var w=window,
d=document,
e=d.documentElement,
g=d.getElementsByTagName('body')[0],

x=w.innerWidth||e.clientWidth||g.clientWidth,
y=w.innerHeight||e.clientHeight||g.clientHeight;

Maybe it helps you too. Now I'm off to self-host the Google Fonts and we are two stops closer to launching ✌️

🙏🙏🙏

Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please ping me on Twitter.

Published