detecting device and OS type using mobile detect js

无人久伴 提交于 2019-12-25 03:24:28

问题


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 LINK and DOC LINK

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?


回答1:


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.

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

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

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.

Other available functions:

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


来源:https://stackoverflow.com/questions/24951414/detecting-device-and-os-type-using-mobile-detect-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!