js判断操作系统windows,ios,android(笔记)

时间:2023-03-09 01:59:28
js判断操作系统windows,ios,android(笔记)

使用JS判断用户使用的系统是利用浏览器的userAgent。

navigator.userAgent:userAgent 获取了浏览器用于 HTTP 请求的用户代理头的值。
 navigator.platform:platform        获取运行浏览器的操作系统和(或)硬件平台。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<script type="text/javascript">
var u = navigator.userAgent;
var iswindows = (u.indexOf("Windows",0) != -1)?1:0;
var ismac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
if (isAndroid==true)
{
self.location=''; #重定向方法
}
if (isiOS==true)
{
self.location='';
}
if (iswindows==true)
{
self.location=''
}
if (ismac==true)
{
self.location=''
}
</script>
</head>
<body>
</body>
</html>