1

I wanna get the width of browser window. It should contain the width of scroll bar if exists.

How to fix this issue via JS?

How to fix this issue via jQuery?

Thank you!

Josh Unger
  • 6,717
  • 6
  • 33
  • 55
weilou
  • 4,419
  • 10
  • 43
  • 56

2 Answers2

6

There are many examples of this scattered around the web. It's a common problem. Here's what I found in a quick search

var winW = 630, winH = 460;
if (document.body && document.body.offsetWidth) {
 winW = document.body.offsetWidth;
 winH = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
    document.documentElement &&
    document.documentElement.offsetWidth ) {
 winW = document.documentElement.offsetWidth;
 winH = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
 winW = window.innerWidth;
 winH = window.innerHeight;
}

http://www.javascripter.net/faq/browserw.htm

Matt Esch
  • 22,661
  • 8
  • 53
  • 51
  • Thank you for your answer! Is there a jQuery solution? I use `$(window).innerWidth()`, but it's incorrect. – weilou Mar 05 '12 at 08:44
  • 1
    As far as I'm aware jQuery doesn't support a fix. innerWidth() is not valid for document or window objects. You are told to use .width() for these objects, but it doesn't include the scrollbars. The best you can do is use jQuery to detect the browser but you shouldn't do this to fix objects. – Matt Esch Mar 05 '12 at 10:50
0

this would fix using a jquery $(window).innerWidth();

Yene Mulatu
  • 2,226
  • 1
  • 17
  • 13