使用移动检测js检测设备和操作系统类型 [英] detecting device and OS type using mobile detect js

查看:288
本文介绍了使用移动检测js检测设备和操作系统类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用设备和操作系统检测脚本,并遇到了此移动检测js插件.看起来很干净,但也很混乱.

I am trying to use a device and OS detection script and came across with this mobile detect js plugin. It seems pretty clean yet quite confusing.

GITHUB链接似乎文档不够详尽,我对如何创建特例感到有些困惑.例如,我想做的是查找用户是使用手机还是平板电脑访问网站,完成检查之后,下一步就是找到设备的操作系统.

It seems the documentation is not thorough and I am finding a bit confusing on how to create special cases. For example, what I am trying to do is to find whether user is visiting website using mobile or tablet and after that check is done the next step is to find the OS of the device.

function deviceDetectionScript() {

    var md = new MobileDetect(window.navigator.userAgent);

    if (md.mobile() && md.phone() && md.tablet()) {

        If(ios) {

            //do something

        }
        If(android) {

            //do something

        }
        If(windows) {

            //do something

        }
    } else {

        // do something else
    }
}

但是,我不确定实现此目标的正确方法.还是有其他方法可以在不使用此脚本的情况下实现类似功能?

However, I am not sure the correct way to achieve this. Or is there any other way around to achieve the similar functionality without using this script?

推荐答案

例如,我是什么 尝试做的是查找用户是否正在使用移动设备访问网站 或表格,完成检查后,下一步是找到操作系统 设备的

For example, what I am trying to do is to find whether user is visiting website using mobile or tables and after that check is done the next step is to find the OS of the device.

仅供参考,平板电脑被认为是可移动的.

Just FYI, tablet is considered mobile.

在做自己想做的事情时,您有两种选择.

You have a couple options when it comes to doing what you want.

要检查用户是否在移动设备上,请使用md.mobile().

To check if user is on mobile use md.mobile().

要确定它是平板电脑还是手机,请使用md.phone()md.tablet().这将返回null或品牌名称.

To determine if it is a tablet or phone use md.phone() and md.tablet(). This returns null or brand name.

最后要检查操作系统,请使用md.os()(它将打印操作系统的名称),或者您可以使用md.is('iPhone')来返回true或false.

Finally to check OS use md.os()(it will print name of OS) or you can use md.is('iPhone') which returns true or false.

其他可用功能:

md.mobile()                   // 'Sony'
md.phone()                    // 'Sony'
md.tablet()                   // null
md.userAgent()                // 'Safari'
md.os()                       // 'AndroidOS'
md.is('iPhone')               // false
md.is('bot')                  // false
md.version('Webkit')          // 534.3
md.versionStr('Build')        // '4.1.A.0.562'
md.match('playstation|xbox')  // false

这篇关于使用移动检测js检测设备和操作系统类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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