Cannot get screen width without scrollbar using expression in cross browser

I use the following expression in JavaScript to get a screen width that seems to work fine, except that it contains the width of the vertical scrollbar when scrolling.

var wMax = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)

Question . How can I change the above expression for the browser so that it displays the width of the screen, excluding the vertical scroll bar? I need to use JavaScript for this, not jQuery.

UPDATE 1:

I found the following cross-browser expression to get the width excluding the vertical scrollbar.

var wMax = Math.min(document.documentElement.clientWidth, window.innerWidth 
                      || screen.width);
  • I used Math.min instead of Math.max, so any width was less than the width
  • and also use screen.width instead of 0 in the original expression, since screen.width will always be the maximum width in this case for all browsers, since it will always include scroll bars.
+4
source share
1 answer

screen.width - This is the width of the display screen, not the browser window, except for the scroll bar.

You should use Math.mininstead Math.max, since the width without the scrollbar will be less than the width with the scrollbars.

Also, if the browser is not working properly, you can try to get the width of the body or html tag without clientWidth, like this one document.documentElement.scrollWidth.

0
source

All Articles