简体   繁体   中英

My “isMMobile ()” function is detecting desktop user agent as mobile

What's this device? My function is detecting this device as mobile

"Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko"

function isMMobile() {

    var device_agent = navigator.userAgent.toLowerCase();
    if (/android|webos|iphone|ipad|ipod|blackberry|windows phone|opera mini|iemobile|tablet|nokia|kindle|mobile/i.test(device_agent)) {
        return true;
    }

    if (window.DocumentTouch && document instanceof window.DocumentTouch) {
        return true;
    }

    if (typeof window.orientation === "number" || 
        'ontouchstart' in window || 
        window.ontouchstart || 
        'ontouchstart' in document.documentElement || 
        "ontouch" in window || 
        window.onmsgesturechange || 
        window.navigator.msMaxTouchPoints || 
        window.navigator.MaxTouchPoints > 0) {
        return true;
    }

    return false;
}

var innerw = window.innerWidth || document.documentElement.clientWidth;
var ismob = isMMobile();

Data sent via ajax:

ismob=yes&innerw=800

That is IE 11 on Windows 7, with a touchscreen. Most windows laptops now have touch (and have for a while) so if your logic is basically “touch == mobile” then you will misidentify a lot of devices. There are a lot of mobile detection scripts out there, which also helps because someone else has to keep up with the new user agents and devices. If you can, let them deal with the headaches in detection!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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