The common ways to get the browser's width are all dependent on whether or not <body>
has a scrollbar. For example, window.innerWidth
, document.documentElement.clientWidth
, $(window).width()
, $(window).outerWidth()
, etc all return different results for the following 2 cases:
<html>
<body>
<p>No scrollbar.</p>
</body>
</html>
and
<html>
<body>
<p style="height:10000px">Yes scrollbar.</p>
</body>
</html>
$(window).width()
, etc, all return a higher value for the first case because the scrollbar's width is subtracted for the second case.
How can I reliably get the browser window's actual width regardless of the scrollbar's existence?
Edit: The suggested possible duplicate's answer says to use $(window).width()
while I specifically said $(window).width()
doesn't work.