How to get browser width using JavaScript code?

Amr Elgarhy picture Amr Elgarhy · Jun 24, 2009 · Viewed 273k times · Source

I am trying to write a JavaScript function to get the current browser width.

I found this one:

javascript:alert(document.body.offsetWidth);

But its problem that it fail if the body has width 100%.

Is there any other better function or a workaround?

Answer

chaos picture chaos · Jun 24, 2009

It's a pain in the ass. I recommend skipping the nonsense and using jQuery, which lets you just do $(window).width().