如何使用JavaScript检测移动设备? [英] How to detect a mobile device with JavaScript?

查看:113
本文介绍了如何使用JavaScript检测移动设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求创建一个实际的HTML页面/ JavaScript来模拟使用JavaScript代码检测移动设备(iPhone / iPad / Android)。这会让用户看到不同的屏幕,询问他们的电子邮件地址。

但其他答案中的 none 确实是100%正确的。如果您想检测用户是否使用任何形式的移动设备(Android,iOS,BlackBerry,Windows Phone,Kindle等),则可以使用以下代码:

  if(/ Android | webOS | iPhone | iPad | iPod | BlackBerry | BB | PlayBook | IEMobile | Windows Phone | Kindle | Silk | Opera Mini / i.test .userAgent)){
//将用户带到不同的屏幕。
}


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.

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆