简体   繁体   English

如何检查正在使用我的网站的设备是否是移动用户

[英]How can I check if the device, which is using my website, is a mobile user or not

I just want to check if the decive, which is using my website, is on a mobile or any other device.我只想检查正在使用我的网站的设备是否在移动设备或任何其他设备上。 It's a quick question with a quick answer I hope.我希望这是一个快速的问题和快速的回答。

If you are trying to see if the user's device is mobile, the MDN docs advices to look for the property maxTouchPoints in the navigator (or browser) object and see if the value is > 0 .如果您尝试查看用户的设备是否是移动设备,MDN 文档建议在导航器(或浏览器)对象中查找属性maxTouchPoints并查看该值是否> 0

In the past this used to be done with User Agent Sniffing ( Read more here ), ie going through the user-agent header sent by the browser into the navigator.userAgent property to see if it contains certain keywords.过去,这通常通过用户代理嗅探来完成( 在此处阅读更多内容),即通过浏览器发送的用户代理标头进入navigator.userAgent属性,以查看它是否包含某些关键字。 This method however has limitations and may not always tell the right kind of device the user is on because many devices today support different browsers and features and vice versa.然而,这种方法有局限性,并且可能无法始终告诉用户使用的是正确类型的设备,因为当今许多设备支持不同的浏览器和功能,反之亦然。

Using User Agent Sniffing (Not recommended today, should be used only as a fallback)使用用户代理嗅探(今天不推荐,应仅用作后备)

var hasTouchScreen = false;

var UA = navigator.userAgent;
        hasTouchScreen = (
            /\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) ||
            /\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA)
        ); 

if (hasTouchScreen) {
    // Device is likely mobile, so do stuff for mobile devices here.
}

Check using maxTouchPoints property and if > 0 in navigator object (MDN Docs Recommended)使用maxTouchPoints属性检查navigator器对象中是否> 0 (推荐 MDN 文档)

var hasTouchScreen = false;

if ("maxTouchPoints" in navigator) {
    hasTouchScreen = navigator.maxTouchPoints > 0;
} 

if (hasTouchScreen) {
    // Device is likely mobile, so do stuff for mobile devices here.
}

Be aware, that not all browsers may support that specification, so the navigator object may not have the property maxTouchPoints or some mobile devices may have large screens and some desktop devices may have small touch-screens or some people may use smart TVs and so on.请注意,并非所有浏览器都支持该规范,因此 navigator 对象可能没有属性maxTouchPoints或者某些移动设备可能具有大屏幕而某些桌面设备可能具有小触摸屏或某些人可能使用智能电视等. So a better way to do this check would be to combine the snippet above with some fallbacks:因此,进行此检查的更好方法是将上面的代码段与一些后备组合起来:

Better way to detect mobile devices using a combination of previous method and fallbacks (Most Robust Method, MDN Docs Recommended)结合使用以前的方法和后备方法来检测移动设备的更好方法(Most Robust Method,MDN Docs 推荐)

var hasTouchScreen = false;

if ("maxTouchPoints" in navigator) {
    hasTouchScreen = navigator.maxTouchPoints > 0;
} else if ("msMaxTouchPoints" in navigator) {
    hasTouchScreen = navigator.msMaxTouchPoints > 0;
} else {
    var mQ = window.matchMedia && matchMedia("(pointer:coarse)");
    if (mQ && mQ.media === "(pointer:coarse)") {
        hasTouchScreen = !!mQ.matches;
    } else if ('orientation' in window) {
        hasTouchScreen = true; // deprecated, but good fallback
    } else {
        // Only as a last resort, fall back to user agent sniffing
        var UA = navigator.userAgent;
        hasTouchScreen = (
            /\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) ||
            /\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA)
        );
    }
}

if (hasTouchScreen)
    // Do something here. 
}

Read more about browser detection using the user agent and the recommended way for mobile device detection here (For the recommended method for mobile device detection, look under the "Mobile device detection" subheading). 在此处阅读有关使用用户代理进行浏览器检测和推荐的移动设备检测方法的更多信息(有关移动设备检测的推荐方法,请查看“移动设备检测”子标题)。

Just use this condition in javascript:只需在 javascript 中使用此条件即可:

if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)){
//do your stuff here
}

暂无
暂无

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

相关问题 如何匹配/了解网站上哪个用户使用了哪个设备? - How can I match/learn which device is used by which user on a website? 如何检查用户是来自我网站的不同页面还是来自其他网站? - How can I check if a user is coming from a different page of my website or from another website? 我如何使网站的移动版本能够在android,iphone,ipad上运行? - How I can make mobile version of my website which will work in android , iphone,ipad? 如何以编程方式检查用户是否允许我的应用在 Google Chrome 中“在此设备上存储文件”? - How can I programatically check if the user has allowed my app to, “Store files on this device,” in Google Chrome? 如何在我的网站的移动视图中添加汉堡包 - How can I add hamburger to my website's mobile view 如何检查用户是否抓住了网站上的滚动条? - How can I check if a user has grabbed a scrollbar on a website? 如何首先检查我的 textarea 并在立即运行网站后使用 JS 显示内容 - How can I check my textarea at first and display something using JS after run the website immediately 我可以从网站上唯一标识移动设备吗? - Can I uniquely identify a mobile device from a website? 如何最好地检测是否通过移动设备访问我的网站 - How to best detect if my website is being accessed through mobile device 我如何使用javascript获取有关用户用来从asp.net Web应用程序获取设备的信息? - How can i get information with javascript about the device which the user used to get on my asp.net webapp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM