Skip to content Skip to sidebar Skip to footer

Screen.width Not Checking For A Window Resize

I'm currently using this script to see if a user that's visiting my site has a screen width smaller than 800. The problem I have is that I also want the alert to be triggered when

Solution 1:

Wrap it in a resize event and you need to get the window's width, not the screen's width

window.addEventListener("resize", function() {
   if (window.innerWidth <= 800) {
       window.alert("screen smaller than 800");
   }
})

Solution 2:

You need:

window.addEventListener('resize', function() {
  if (window.innerWidth <= 800) {
    window.alert("screen smaller than 800");
  }
}, true);

Solution 3:

You can dod this with the resize function:

var width = $(window).width();

$(window).resize(function(){
  if ($(this).width() <= 800) {
    window.alert("screen smaller than 800");
  }   
});

Post a Comment for "Screen.width Not Checking For A Window Resize"