用js获取操作系统

JavaScript014

用js获取操作系统,第1张

case "6.2": document.write("Windows 8")break

case "6.3":document.write("Windows 8.1")break

那是因为ua[2]的值为“ Windows NT 5.1”注意前面有个空格。

使用indexOf方法判断就可以了。

<script language="javascript">

var ua = navigator.userAgent.split("")

if(ua[2].indexOf("Windows NT 5.1")!=-1)

{

window.location = 'http://www.baidu.com'

}

else

{

window.location = 'http://www.google.com'

}

//document.write("'"+ua[2]+"'")

</script>