Make code only work on Desktop [duplicate]

1

How do I make a code, JQuery / JavaScript only work on Desktop? That is, in mobile phones and tablets, I do not want it working ... With CSS I use Media Query, but with JavaScript I get a little lost.

Thank you!

    
asked by anonymous 05.05.2017 / 14:50

1 answer

1

If the case depends on the screen size , you can just do it:

if ($(window).width() > 960) {
   alert('ecrã é maior que 960');
}

If the case really depends on whether you are a desktop browser or not, you can do this as suggested in this question of Stack Overflow in English:

$(document).ready(function() {
    var isDesktop = (function() {
        return !('ontouchstart' in window) // works on most browsers 
        || !('onmsgesturechange' in window); // works on ie10
    })();
    //edit, if you want to use this variable outside of this closure, or later use this:
    window.isDesktop = isDesktop;
    if( isDesktop ){ /* desktop things */ }
});
    
05.05.2017 / 15:38