下载大厂的安装包的时候,很多智能的依据操作系统直接返回
dmg
文件或者exe
文件,明明网站也也没有让用户选择目前的操作系统。原因就是window.navigator
对象。
打开Chrome控台,查看window.navigator
其中常用的就是userAgent
属性,里面可以监测到浏览器的名称、版本以及操作系统。
一种判断方法:
function getOS () {
if (navigator.userAgent.indexOf('Window') > 0) {
return 'Windows'
} else if (navigator.userAgent.indexOf('Mac OS X') > 0) {
return 'Mac '
} else if (navigator.userAgent.indexOf('Linux') > 0) {
return 'Linux'
} else {
return 'NUll'
}
}
复习下:
BOM
的顶层就是window
,window
下包括history
、location
、navigator
、screen
、document
等子对象,其中document
就是DOM