Detecting mobile browsers on the web?
Asked Answered
A

4

4

I'm curious to know how to check for iPhone, iPad and other mobile browsers.(JavaScript or CSS)

Edit:

Not user agent string, please. That can be faked.

Possible Dupes:

Abigail answered 3/9, 2010 at 0:3 Comment(3)
possible duplicate of Standard way to detect mobile browsers in a web application based on the http requestRabbinate
So what if it can be faked? If thats that case than the users intention was to view the web that way anyway.Lacto
I'm afraid there is no other way to detect a browser other than reading it's user agent string.Freeload
S
2

Basically you check the User Agent String

see http://www.hand-interactive.com/resources/detect-mobile-javascript.htm

Detect iPhone:

navigator.userAgent.toLowerCase().search("iphone") > -1

In general feature detection is better than browser detection it is better to know what the user's browser can do than what he's using. Modernizer is a good tool for that.

Subchaser answered 3/9, 2010 at 0:10 Comment(1)
@Abigail No. Change iPhone to iPad to detect iPadSubchaser
A
7

I would use WURFL. It's an Open Source xml-database of more than 10000 mobile devices that will detect (almost always) your Mobile Phone and Browser capabilities given the user-agent HTTP header value.

You will get information like:

  1. Screen size
  2. XHTML/HTML support level
  3. Graphic type support

Many others.

There are wrapper APIs for popular languages such as PHP, Java and .NET, so you won't have to deal with the XML database yourself.

Acedia answered 3/9, 2010 at 0:4 Comment(0)
S
2

Basically you check the User Agent String

see http://www.hand-interactive.com/resources/detect-mobile-javascript.htm

Detect iPhone:

navigator.userAgent.toLowerCase().search("iphone") > -1

In general feature detection is better than browser detection it is better to know what the user's browser can do than what he's using. Modernizer is a good tool for that.

Subchaser answered 3/9, 2010 at 0:10 Comment(1)
@Abigail No. Change iPhone to iPad to detect iPadSubchaser
M
1

Use Modernizr - http://modernizr.com/

Modernizr is a JS script that tests the browser for a variety of HTML5 and CSS3 capabilities when the page loads. You can either look in the Modernizr JS object, or use the classes it adds to the HTML element. If the class 'touch' is present, you have a touchscreen device; otherwise, the class is no-touch. Then you can do this in your CSS

.touch .myElement { /* touch device styles */ }

.no-touch .myElement { /* regular browser styles */ }

Testing a browsers capabilities is far more useful and future-proof that sniffing for user agents. In this way, for each CSS3 feature you want to add, you can easily write a fallback, as I show here.

Moldboard answered 21/9, 2012 at 21:3 Comment(0)
E
0

Use javascript to detect the HTTP User Agent - fancy term for name of the browser

Elviselvish answered 3/9, 2010 at 0:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.