Question

How do I know whether browsing the site (built in HTML5), done by a computer or by a cell phone devices (iPhone, Android, etc.)

I know there is such thing as User Agent, but I want to know his specific line of code ..

What it shows when I'm on a computer, and what it shows when I'm on mobile phones?

Thank you

Was it helpful?

Solution

You can do this.

   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());
        }
    };

More information on this link.

But if javascript is disabled you wouldn't be able to do it like this however, you can perform validations or checking on the server side. but that's now up to you. Depending on the CGI you are using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top