js判斷Android和Ios

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Android&IOS</title>
</head>

<body>
<button id='btn' onclick="test()">點擊</button>
</body>

</html>
<script type="text/javascript">
var u = navigator.userAgent;
console.log(navigator.userAgent.match())
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android終端

var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端
function test() {
if (isAndroid == true) {
console.log("安卓手機")
} else if(isiOS == true) {
console.log("蘋果手機")
}else{
console.log("不是手機")
}
}
</script>


&&&&&&&&&&&&&&&&&&&&&&&&

// 判斷安卓function isAndroid() {    var u = navigator.userAgent;    if (u.indexOf("Android") > -1 || u.indexOf("Linux") > -1) {        if (window.ShowFitness !== undefined) return true;    }    return false;}// 判斷設備爲 iosfunction isIos() {    var u = navigator.userAgent;    if (u.indexOf("iPhone") > -1 || u.indexOf("iOS") > -1) {        return true;    }    return false;}
相關文章
相關標籤/搜索