check if browser is on mobile device using javascript

i have to write a javascript/html5 website. I have to check if the client browser is opened on a mobile device (like ipad , iphone, android etc)
how can i do that using javascript? pl guide...
thanks a lot
ts84zsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
If you only need to detect if client browser is mobile device, try this script:
function isMobileDevice() { 
   if ( navigator.userAgent.match(/Android/i) ||
        navigator.userAgent.match(/webOS/i) ||
        navigator.userAgent.match(/iPhone/i) ||
        navigator.userAgent.match(/iPad/i) ||
        navigator.userAgent.match(/iPod/i) ||
        navigator.userAgent.match(/BlackBerry/i) ||
        navigator.userAgent.match(/Windows Phone/i) ||
        navigator.userAgent.match(/IEMobile/i) ||
        navigator.userAgent.match(/Opera Mini/i)
        )
 {
       return true;
 }
 else {
       return false;
 }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ts84zsAuthor Commented:
OK thanks a lot....

That javascript code works for all the browsers?
Duy PhamFreelance IT ConsultantCommented:
Yes, I believe it should work for most common mobile browsers. There might be some small mobile manufacturers making their own mobile browsers, but we don't really need to care about those until their devices have bigger market shares.
ts84zsAuthor Commented:
thanks a lot
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Responsive Web

From novice to tech pro — start learning today.