Вопрос

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.

Это было полезно?

Решение

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!

Другие советы

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.

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>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top