h5页面判断移动端系统为Android或IOS


  最近遇到了一个需求,即所谓的 app+web 混合开发,需要将 h5 内嵌到 APP 中,这个时候因为要对 Android 和 IOS 有不同的处理逻辑,所以我们就需要判断一下,移动端的系统到时是哪一种。下面我们就提供一下判断型号的方法,代码如下:

function detect(){
    let equipmentType = "";
    let agent = navigator.userAgent.toLowerCase();
    let android = agent.indexOf("android");
    let iphone = agent.indexOf("iphone");
    let ipad = agent.indexOf("ipad");
    if(android != -1){
        equipmentType = "android";
    }
    if(iphone != -1 || ipad != -1){
        equipmentType = "ios";
    }
    return equipmentType;
}

  

  在上面的方法中,返回值为 android 则为 Android 系统,返回值为 ios 则为 IOS 系统。判断完型号,就可以执行对应操作了。

if(detect()==='android'){
    //对Android系统的移动端页面做点什么


}else if(detect()==='ios'){
    //对IOS系统的移动端页面做点什么


} 

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM