最近遇到了一個需求,即所謂的 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系統的移動端頁面做點什么 }