How do I check if my application is open on mobile or web?

Asked

Viewed 541 times

4

I need to do a.css style for mobile and another for web .

2 answers

3

I don’t know anything that separates totally mobile from desktop, because you have many factors that can influence this, for example, there are still mobile that do not have touch, there is the Aspect ratio of the device where you can not simply say that 800px is mobile or not. everything depends on what you need to do!

If you just want to make a responsive layout you can delimit min or max width via media query. recommend these two readings: Tableless and MDN.

Now, if you want to check the touch issue you can use this code in javascript:

if(('ontouchstart' in window)){
    //seu cod. aqui!
}

If you can define better what you need I can help you further.

  • I think in practice what most do is detect on the server side whether the device is mobile, and sends a different page, or a different CSS at least, depending on the type. In PHP I use the class http://mobiledetect.net/

  • Excellent comment epx! you also have this option Jose, everything depends on what you need, I particularly seek to do these checks in the client-side, but nothing prevents you to do this check on the server as well.

  • which client-side check you use @Dorivalzanetto ?

  • It depends a lot of what I need Jose, for example if I need a modification to Landscape/Portrait I use the orientation of the media query, if it is to make a responsive for small screens I use the min-width or max-width as the value of the query, this goes for height too, If it’s touch I use the Js section I sent you above.. As I told you, it all depends on what I need, a look at the references I showed you! What you necessarily need to do?

-1


function isMobile() {
   return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER['HTTP_USER_AGENT']);
}

Staff has a script that detects where your application is being accessed follow the link below : http://wbruno.com.br/expressao-regular/detectando-browser-mobile-javascript/

  • Could you bring some of the link content to the body of your answer? Links constantly break, so responses based only on the link end up being obsolete faster and are removed. However, it is always good to leave the link as a reference :)

Browser other questions tagged

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