jquery $(window).width() and $(window).height() return different values when viewport has not been resized jquery $(window).width() and $(window).height() return different values when viewport has not been resized jquery jquery

jquery $(window).width() and $(window).height() return different values when viewport has not been resized


I think what you're seeing is the hiding and showing of scrollbars. Here's a quick demo showing the width change.

As an aside: do you need to poll constantly? You might be able to optimize your code to run on the resize event, like this:

$(window).resize(function() {  //update stuff});


Try to use a

  • $(window).load event

or

  • $(document).ready

    because the initial values may be inconstant because of changes that occur during the parsing or during the DOM load.


Note that if the problem is being caused by appearing scrollbars, putting

body {  overflow: hidden;}

in your CSS might be an easy fix (if you don't need the page to scroll).