2
votes

How to check if a web page has been opened by a mobile browser or a computer browser. I tried this :

name = request.getHeader("User-Agent");

But it gives these type of results :

  • For Fedora Firefox Browser : Mozilla/5.0 (X11; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0
  • For Android Phone Browser : Mozilla/5.0 (Linux;U;Android 2.3.6;en-in;Micromax A50 Build/GRK395) AppleWebKit/533.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1

I cannot from the above two results,differentiate whether the web page was visited by a mobile phone or a computer. How do I do that ?

It will be great if I am able do this in Java.

4
why you can't. In second case you have Androidmishadoff
So try searching for all known mobile OS names and you will have your answer:)Adel Boutros
@grasPro if it Android with high probability it is from phone. Use Adel's advise to get more mobile OSmishadoff
@mishadoff How did you calculate the probability ? What was the estimate you made ,of android tablets while calculating the probability ?saplingPro
can you check for window width like in css media queries for responsive design?pcarvalho

4 Answers

3
votes

Like Mikko wrote, no simple ways to detect mobile.

But you can try: http://code.google.com/p/php-mobile-detect/

And something like previous answer: http://detectmobilebrowsers.com/

2
votes

There is not any simple 'mobile=true' flag. You simply have to check by yourself. From here you can find subset of of values to search (list is rather old, so newer mobile browsers should be added).

1
votes

this is a good link I just found:

http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript/

var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
       return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() ||      isMobile.Opera() || isMobile.Windows());
    }
};
0
votes

You could also look at the size of the screen (smaller size would possibly mean a mobile device), which should be accessible through javascript.