jQuery function should only be executed on mobile

3

I think the title is self-explanatory. I need a jQuery function to "enable" only when I am in a resolution less than 1024px.

    
asked by anonymous 13.11.2014 / 16:35

1 answer

7

You can detect the screen size and perform the function:

screen.width and screen.height respectively store the width and height of the screen.

if(screen.width < 1024){ 
   //executa a funcao para ativar
}

Another way I found here using javascript is like this:

The function returns true if the browser used is mobile and false otherwise.

function isMobile() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.search(/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i) != -1) {
    return true;
  }
  return false;
}
    
13.11.2014 / 16:39