There are two methods to get the viewport width:
document.documentElement.clientWidth. The former is more accurate. The latter has better browser support.
To get the best of both worlds, try
innerWidth first, and fallback to
clientWidth if not supported.
var viewportWidth = window.innerWidth || document.documentElement.clientWidth;
Works in all modern browsers, and IE9+. You can extend support back further with polyfills.