Make code only work on Desktop

Asked

Viewed 349 times

1

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

Thank you!

  • 2

    What is the usefulness of this? From the user’s point of view, it is an arbitrary (and annoying) limitation. From a security point of view, it is useless, since any solution will depend on the user’s browser to honor some standard that can be swindled easily.

  • 1

    Pablo, it’s just that it has effects that I don’t want to work on the phone, it’s different, popups too. I want to make the phone as simple as possible. I have a store, they are common and lay users. With CSS Media Query, there is no way to cheat, put in such size the effect does not appear or is different. So I just wanted the same thing for Javascript

1 answer

1


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

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

If the case really depends on being a desktop browser or not, you can do it this way as suggested in this question 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 */ }
});
  • 2

    My PC has touch screen. This check by ontouchstart would cause the server to give me a mobile experience on a fifteen inch screen.

  • 1

    I kept the $(window). width(); I thought better, and I think it will work like the CSS Media Query.

  • Truth @Renan , I didn’t think of it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.