2

How would I go about determining the device that the viewer (which would be iOS) that the user is holding? In JavaScript? IE iPad, iPod, iPhone ect.

Thanks very much! Cheers

Edit: I would like to echo the device name, IE ipad IE ipod IE iphone... ect

Edit: Added the PHP tag, as some of the answers which I really like use PHP.

  • Detecting it from where? From the client or the server? – Jonathan M Jan 05 '13 at 02:12
  • 2
    `navigator.userAgent` differs for each of these, if that's your question – John Jan 05 '13 at 02:13
  • @JonathanM client. I mean I don't think that servers can run on iOS... –  Jan 05 '13 at 02:14
  • @SolèneLeVan, sorry, the way I read it, you might have been asking how to detect which OS the browser is running on. The detection method, of course, varies depending on whether you're detecting from the client or the server. – Jonathan M Jan 05 '13 at 02:16
  • @JonathanM It's always going to be the from the client if it's using javascript... – John Jan 05 '13 at 02:17
  • It would help us if you could clarify your objective - I.e. what do you wish to do based on this information? – PassKit Jan 05 '13 at 02:17
  • document.write it... ie, if the device is ipad it writes ipad if ipod writes ipod if iphone write iphone. ect –  Jan 05 '13 at 02:18
  • @John, yeah, most of the time. Didn't want to assume because of stuff like this: http://stackoverflow.com/questions/7626405/how-to-write-a-small-socket-server-in-ios – Jonathan M Jan 05 '13 at 02:18
  • Then `navigator.userAgent` should do the job. – PassKit Jan 05 '13 at 02:19
  • @JonathanM yeah that post's interesting... I had no clue that anything of the sort was possible on ios –  Jan 05 '13 at 02:19
  • @SolèneLeVan, yep. It's why I asked. – Jonathan M Jan 05 '13 at 02:20
  • 1
    Check [**The MobileESP Project**](http://blog.mobileesp.com) ([here's the download page](http://code.google.com/p/mobileesp/)) for a JS solution and [**Mobile-Detect**](https://github.com/serbanghita/Mobile-Detect) for a PHP solution. – inhan Jan 05 '13 at 05:32
  • @inhan wow good response! do you want to post as an answer so I can mark as correct? –  Jan 05 '13 at 06:27
  • 1
    sure, made it an answer. @arttronics' answer is also good, though. – inhan Jan 05 '13 at 15:14

3 Answers3

1

jsFiddle DEMO ← Visit with your mobile device!

Remove /show/ in URL Address Bar to access the jsFiddle edit page.

http://detectmobilebrowsers.com/

The above open source method, DetectMobileBrowers plugin, contains the source you'll need, in a variety of formats, to quickly identify the browser and mobile devices including iphone / ipod.

The homepage above is also a Live Demo to check out which device / useragent your using.

Also, if you do use that script note Boolean true or false on the variable is to be used (i.e., use !! before variable name to test for false).


EDIT: The Supported Devices Section contains a downloadable archive of all devices and user agents detected. Mentioned there is a mod to include tablet support (e.g., iPad Tablets).

Sample Listing:

apple_generic_iphoneos_mmsclient
apple_ipad_ver1
apple_ipad_ver1_sub42
apple_ipad_ver1_sub43
apple_ipad_ver1_sub431
apple_ipad_ver1_sub5312110
apple_ipad_ver1_subiprod
apple_ipad_ver1_subsimulator
apple_iphone_coremedia_ver1
apple_iphone_coremedia_ver1_1_5
apple_iphone_coremedia_ver1_sub5a347
apple_iphone_coremedia_ver1_sub5b108
apple_iphone_coremedia_ver1_sub5f136
apple_iphone_coremedia_ver1_sub5f137
apple_iphone_coremedia_ver2_1_1
apple_iphone_coremedia_ver2_2
apple_iphone_coremedia_ver2_2_1
apple_iphone_coremedia_ver2_2a
apple_iphone_emulator_ver2
apple_iphone_emulator_ver2_1
apple_iphone_emulator_ver2_2_1
apple_iphone_emulator_ver2_sub52520
apple_iphone_emulator_ver2_sub52520_5f135
apple_iphone_emulator_ver3
apple_iphone_emulator_ver3_1
apple_iphone_emulator_ver4
apple_iphone_emulator_ver5
apple_iphone_ver1
apple_iphone_ver1_china
apple_iphone_ver1_os201
apple_iphone_ver1_somesdk
apple_iphone_ver1_somesdk_subenus
apple_iphone_ver1_sub1a542a
apple_iphone_ver1_sub1c25
apple_iphone_ver1_sub1c28
apple_iphone_ver1_sub3a109a_dadk
apple_iphone_ver1_sub3a109a_plpl
apple_iphone_ver1_sub3a109a_svse
apple_iphone_ver1_sub3a109a_zhcn
apple_iphone_ver1_sub3b48a
apple_iphone_ver1_sub3b48b
apple_iphone_ver1_sub3b48b_nlnl
apple_iphone_ver1_sub3b48b_ptpt
apple_iphone_ver1_sub3b48b_trtr
apple_iphone_ver1_sub3b48b_zhtw
apple_iphone_ver1_suba543
apple_iphone_ver1_suboperamini5
apple_iphone_ver2
apple_iphone_ver2_0_2
apple_iphone_ver2_0_2_subua
apple_iphone_ver2_1
apple_iphone_ver2_1_1
apple_iphone_ver2_1_sub525181
apple_iphone_ver2_1_sub525181_subua
apple_iphone_ver2_1_subcydia
apple_iphone_ver2_2
apple_iphone_ver2_2_1
apple_iphone_ver2_2_1_svse
apple_iphone_ver2_2_sub525181
apple_iphone_ver2_2_sub5g77
apple_iphone_ver2_2_subcydia
apple_iphone_ver2_2_subua
apple_iphone_ver2_sub525181
apple_iphone_ver2_sub5a347
apple_iphone_ver2_sub5b108
apple_iphone_ver3
apple_iphone_ver3_0_1
apple_iphone_ver3_1
apple_iphone_ver3_1_0
apple_iphone_ver3_1_0_sub52816
apple_iphone_ver3_1_1
apple_iphone_ver3_1_2
apple_iphone_ver3_1_2_svse
apple_iphone_ver3_1_3
apple_iphone_ver3_1_3_subenus
apple_iphone_ver3_1_sub7c116a
apple_iphone_ver3_sub7a259g
apple_iphone_ver3_sub7a312g
apple_iphone_ver3_sub7a341
apple_iphone_ver3_sub7a341_enus
apple_iphone_ver4
apple_iphone_ver4_1
apple_iphone_ver4_1_sub8b113
apple_iphone_ver4_1_sub8b117
apple_iphone_ver4_1_sub8b117_cydia
apple_iphone_ver4_1_subgoog_bot
apple_iphone_ver4_2
apple_iphone_ver4_2_1
apple_iphone_ver4_2_5
apple_iphone_ver4_2_6
apple_iphone_ver4_2_7
apple_iphone_ver4_2_8
apple_iphone_ver4_3
apple_iphone_ver4_3_1
apple_iphone_ver4_3_2
apple_iphone_ver4_3_3
apple_iphone_ver4_3_4
apple_iphone_ver4_3_5
apple_iphone_ver4_sub401
apple_iphone_ver4_sub405
apple_iphone_ver4_sub405_eses
apple_iphone_ver4_sub533179
apple_iphone_ver5
apple_iphone_ver5_1
apple_iphone_ver5_subua
apple_ipod_touch_ver1
apple_ipod_touch_ver1_fr
apple_ipod_touch_ver1_no_u
apple_ipod_touch_ver1_os201
apple_ipod_touch_ver1_os211
apple_ipod_touch_ver1_sub3b48b_dadk
apple_ipod_touch_ver1_sub3b48b_fr
apple_ipod_touch_ver1_sub3b48b_jajp
apple_ipod_touch_ver1_sub3b48b_kokr
apple_ipod_touch_ver1_sub3b48b_nlnl
apple_ipod_touch_ver1_sub3b48b_svse
apple_ipod_touch_ver1_sub3b48b_zhcn
apple_ipod_touch_ver1_sub3b48b_zhtw
apple_ipod_touch_ver1_sub4193
apple_ipod_touch_ver1_sub4a102
apple_ipod_touch_ver1_sub4a93_plpl
apple_ipod_touch_ver1_sub4a93_ptpt
apple_ipod_touch_ver1_sub4b1
apple_ipod_touch_ver1_subenus_sub5f137
apple_ipod_touch_ver2
apple_ipod_touch_ver2_1
apple_ipod_touch_ver2_1_engb
apple_ipod_touch_ver2_1_subenus_sub5f138
apple_ipod_touch_ver2_2
apple_ipod_touch_ver2_2_1
apple_ipod_touch_ver2_2_1_sub5h11
apple_ipod_touch_ver2_2_1_sub5h11a
apple_ipod_touch_ver2_2_1_subengb
apple_ipod_touch_ver2_2_sub5g77a
apple_ipod_touch_ver2_sub525181
apple_ipod_touch_ver2_sub5a347
apple_ipod_touch_ver2_sub5c1
apple_ipod_touch_ver3
apple_ipod_touch_ver3_1
apple_ipod_touch_ver3_1_1
apple_ipod_touch_ver3_1_1_subua
apple_ipod_touch_ver3_1_2
apple_ipod_touch_ver3_1_2_sub7d11
apple_ipod_touch_ver3_1_2_subua
apple_ipod_touch_ver3_1_3
apple_ipod_touch_ver3_1_3_subua
apple_ipod_touch_ver3_1_subua
apple_ipod_touch_ver3_subua
apple_ipod_touch_ver4
apple_ipod_touch_ver4_1
apple_ipod_touch_ver4_1_subua
apple_ipod_touch_ver4_2_1
apple_ipod_touch_ver4_2_1_subua
apple_ipod_touch_ver4_3
apple_ipod_touch_ver4_3_1
apple_ipod_touch_ver4_3_1_subua
apple_ipod_touch_ver4_3_2
apple_ipod_touch_ver4_3_2_subua
apple_ipod_touch_ver4_3_3
apple_ipod_touch_ver4_3_3_subua
apple_ipod_touch_ver4_3_4
apple_ipod_touch_ver4_3_5
apple_ipod_touch_ver4_3_subua
apple_ipod_touch_ver4_sub402
apple_ipod_touch_ver4_sub402ua
apple_ipod_touch_ver4_subua
apple_ipod_touch_ver5
apple_ipod_ver1

After Accepted Answer Status:
As an alternate to DetectMobileBrowsers plugin, the other SO Answer on this page by inhan for The MobileESP Project does something similar. For future visitors to this Question, check them both out. Cheers!

Community
  • 1
  • 1
arttronics
  • 9,957
  • 2
  • 26
  • 62
  • Solène Le Van, can you please clarify if [**this User Vendor jsFiddle**](http://jsfiddle.net/k4CGt/show/) solves your issue **also?** I ask because if it works for you, I'll include that in my answer because **that uses no plugin** at all. Thanks! – arttronics Jan 06 '13 at 01:57
1

Here are the ones I know. Sometime ago I found The MobileESP Project (here's the download page), a Javascript solution. It looked good at the time I found it so I kept a copy but I haven't used it yet so I can't say much about it.

Server-side (which is more reliable, I believe) I've already used the Mobile-Detect class (PHP) in some projects. It looks like the most accurate and detailed option out there.

Hope that helps.

inhan
  • 7,394
  • 2
  • 24
  • 35
  • Yes it definitely does! :) (I'm best at PHP so this is cool...) I didn't even know that something like this was possible with PHP! I thought only JavaScript could do browser stuff like this... –  Jan 05 '13 at 17:31
  • The answer, because it uses PHP, is my favorite, but arttronics is soo detailed... :( Which one should I pick as correct? –  Jan 05 '13 at 17:34
  • @SolèneLeVan Choose his'. No problem ;) – inhan Jan 05 '13 at 17:38
  • @SolèneLeVan also, you might want to edit your question and add the PHP tag so that other users looking for the same solution can easily find your question. – inhan Jan 05 '13 at 17:40
  • **+1** For turning your comment into an answer. I like that there is another choice that works (In my answer I mention this one). Also, detectmobilebrowsers does PHP and so many other types and it's open source. Cheers – arttronics Jan 05 '13 at 22:10
  • @arttronics I'll check that also. Good to learn there's a similar project out there - just in case. Thanks for sharing it and the upvote. Cheers. – inhan Jan 06 '13 at 01:30
  • +1 I just chose "arttronic"'s response as an answer, but I upvoted yours. And I added the PHP tag. :) –  Jan 06 '13 at 01:59
0

If you're looking for simple Javascript user agent detection, this should do the job.

<script type="text/javascript">
if(navigator.userAgent.match(/iPod/i))
    //Found an iPod!
else if(navigator.userAgent.match(/iPad/i))
    //Found an iPad!
else if(navigator.userAgent.match(/iPhone/i))
    //Found an iPhone!
</script>
John
  • 2,675
  • 2
  • 18
  • 19