简体   繁体   English

使用 Javascript 或 JQuery 检测手机

[英]Detect Mobile PHONE with Javascript or JQuery

I am not interested in Tablets or mobile devices.我对平板电脑或移动设备不感兴趣 Just if the user is visiting the page with a phone.就像用户使用手机访问页面一样。

I am not looking for something 100% perfect.我不是在寻找 100% 完美的东西。 But a best effort would be nice.但尽最大努力会很好。 If I show a sms 'a' tag link and you are NOT on a phone... it's not the end of the world but I would like to do the best I can.如果我显示一个短信“a”标签链接,而您不在电话上……这不是世界末日,但我想尽我所能。

Any ideas on how to specifically try to filter out for phones even if it is not 100% perfect?关于如何专门尝试过滤掉手机的任何想法,即使它不是 100% 完美?

Thanks谢谢

There are perhaps a couple of approaches you could take to this - both would be making massive assumptions and be unreliable at best!也许您可以采取几种方法来解决这个问题 - 两者都会做出大量假设并且充其量是不可靠的!

The first approach would be a responsive media query.第一种方法是响应式媒体查询。 You could presume that a mobile phone has a screen size greater than x and less than y.您可以假设手机的屏幕尺寸大于 x 且小于 y。 For example:例如:

@media only screen and (min-width: 320px) and (max-width: 600px) {}

I'd improve on that to also check for a height, and find some statistics on common device sizes to put sensible breakpoints in there.我会改进它以检查高度,并找到一些关于常见设备尺寸的统计数据,以在那里放置合理的断点。 You also need to account for device orientation, when a device is landscape orientated it'll be wider than usual.您还需要考虑设备方向,当设备横向时,它会比平时更宽。

Your other option is to use the user-agent string.您的另一个选择是使用用户代理字符串。 Both Apple and Android devices specifically state if it's a mobile version of the device. Apple 和 Android 设备都特别说明它是否是设备的移动版本。 On iOS you can look for the phrase 'iphone'.在 iOS 上,您可以查找短语“iphone”。 On android you can look for 'mobile safari'.在 android 上,您可以查找“移动 safari”。 Windows phone, and other devices likely have similar detects, you'd need to use emulators to examine each UA string and then construct a suitable regex. Windows 手机和其他设备可能有类似的检测,您需要使用模拟器来检查每个 UA 字符串,然后构建一个合适的正则表达式。

A final possibility, if you're interested specifically in the device's ability to handle telephone operations might be to run a detect against a telephone number.最后一种可能性,如果您对设备处理电话操作的能力特别感兴趣,可能是针对电话号码运行检测。

Create an element on the page that contains a phone number.在页面上创建一个包含电话号码的元素。 In iOS (and likely similarly on other platforms), this will get modified so it has an automatic href added with tel: and then your number.在 iOS 中(在其他平台上可能类似),这将被修改,因此它会自动添加一个带有 tel: 的 href,然后是您的号码。 You could draw this element off screen, see if the number gets auto-formatted, and presume a mobile phone if so.您可以在屏幕外绘制此元素,查看号码是否自动格式化,如果是,则假设是手机。 I imagine an iPad will also do this, but then an iPad will also allow you to send SMS if you have text message forwarding enabled.我想 iPad 也能做到这一点,但如果您启用了短信转发功能,iPad 也将允许您发送短信。

I'd suggest doing it server side if you want to manipulate the view for the end user, however I wrote a small JS library that enables this sort of checking a while back - take a look: https://github.com/dj10dj100/what_browser如果您想为最终用户操作视图,我建议在服务器端执行此操作,但是我编写了一个小型 JS 库,可以在一段时间后进行此类检查 - 看看: https : //github.com/dj10dj100 /what_browser

Along other things you'll be able to check if the browser is a mobile using js : if(what.device.isMobile() == true){ //Mobile device }除此之外,您还可以使用 js 检查浏览器是否是移动设备: if(what.device.isMobile() == true){ //Mobile device }

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
 // some code..
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM