How does js prepare to judge useragent information?

for example, my website is divided into
pc www.mysite.com
Mobile m.mysite.com

I want to realize that when a user browses my pc version of www.mysite.com through a mobile device, it can determine whether the user is browsing on a mobile device through js, and transfer to the corresponding mobile version of m.mysite.com

.

how to write this js, is there any mature plug-in and so on? please advise, thank you!

Mar.11,2022

/**
 * 
 * @returns {boolean}
 */
const isMobile = () => {
  const u = navigator.userAgent,
    mobile = !!u.match(/AppleWebKit.*Mobile.*/), //
    ios = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios
    android = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //androiduc
    iPhone = u.indexOf('iPhone') > -1, //iPhoneQQHD
    iPad = u.indexOf('iPad') > -1; //iPad

  return mobile || ios || android || iPhone || iPad;
};

/**
 * 
 * @returns {boolean}
 */
const isWechat = () => {
  const ua = navigator.userAgent.toLowerCase();
  return ua.match(/MicroMessenger/i) == 'micromessenger';
};

Mobile judgment function I am currently using




var u = navigator.userAgent, app = navigator.appVersion;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;//
var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);//ios
// 
    function is_weixn() {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            return true;
        } else {
            return false;
        }
    }
    //PC
    function IsPC() {
        var userAgentInfo = navigator.userAgent;
        var Agents = ["Android", "iPhone",
            "SymbianOS", "Windows Phone",
            "iPad", "iPod"];
        var flag = true;
        for (var v = 0; v < Agents.length; vPP) {
            if (userAgentInfo.indexOf(Agents[v]) >= 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }


js useragent


try this?
https://github.com/fex-team/u.

Menu