JavaScript test that I am returning to a smartphone?

I want to check in one of my JavaScript functions, which determines if I am on a smartphone, and then decide whether to run the function based on the results.

What is the best way to detect / check your smartphone (or handheld device in general) for JavaScript?

eg.

if (user is on a smartphone) { //run this code if on a phone } else { //run this code if not on a phone } 

If there is no specific method for checking the phone, then what is the best choice of checks I must perform before deciding which code to run?

EDIT / UPDATE: Answers and comments on similar posts should give me something to continue - What is the best way to detect a mobile device in jQuery?

+4
source share
3 answers

Ye olde browser sniffing seems to be the solution

 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) ){ // some code.. } 
+1
source

You can try twitter bootstrap, http://twitter.github.com/bootstrap/scaffolding.html . In particular, the section "Revision Design".

0
source

You can use the JA Categorizr library to check if the user device is a mobile phone, tablet, smart TV or desktop. Categorizr uses sniffing a user agent, so you should keep an eye on script updates as user agents tend to “evolve” over time.

Here's how you could use Categorizr to check if the user is on a smartphone, but not a smart TV, desktop, or tablet:

 if (categorizr.isMobile) { //run this code if on a phone } else { //run this code if not on a phone } 
0
source

All Articles