如何使用 JavaScript 检测移动设备? [英] How to detect a mobile device with JavaScript?
问题描述
我被要求创建一个实际的 HTML 页面/JavaScript 来模拟使用 JavaScript 代码检测移动设备(iPhone/iPad/Android).然后,这会将用户带到一个不同的屏幕,要求他们提供他们的电子邮件地址.
I have been asked to create an actual HTML page / JavaScript to simulate detection of the mobile devices (iPhone / iPad / Android) using JavaScript code. This will then take the user to a different screen which asks them for their email address.
推荐答案
我知道这个答案迟到了 3 年,但其他答案没有确实是 100% 正确的.如果您想检测用户是否使用任何形式的移动设备(Android、iOS、黑莓、Windows Phone、Kindle 等),则可以使用以下代码:
I know this answer is coming 3 years late but none of the other answers are indeed 100% correct. If you would like to detect if the user is on ANY form of mobile device (Android, iOS, BlackBerry, Windows Phone, Kindle, etc.), then you can use the following code:
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent)) {
// Take the user to a different screen here.
}
这篇关于如何使用 JavaScript 检测移动设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!