Pergunta

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

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top