var windowRes = document.documentElement.clientWidth; if (windowRes <= 1024) { console.log('hello') if(windowRes <= 768) { console.log('yes'); } } 

There is an idea, but for some reason 'yes' is not displayed on 768. Any idea why?

  • window roll up to narrower. And do not miss the semicolon - Jean-Claude
  • There is a sure way to check the performance of the code - run it for execution. - Sergey

1 answer 1

clientWidth = width of the window, not the screen.
Accordingly, from 1024 * 768 everything is fine, there will be about 1000, but 800x600 is already worse - it is unlikely there will be less than 768, so the nested if will not work.

Just look at the actual clientWidth value in each case.