screen和navigator是window對(duì)象下的兩個(gè)對(duì)象解总,可以不帶window.直接使用screen和navigator懦底。screen包含瀏覽器窗口的信息鲜屏,navigator包含瀏覽器及系統(tǒng)的信息吱抚。
screen對(duì)象
window.screen 對(duì)象包含有關(guān)用戶屏幕的信息俺陋,使用的時(shí)候可以不帶window豁延。下面代碼我們?cè)诳刂婆_(tái)打印一下screen昙篙,screen對(duì)象主要有的是尺寸的屬性。
console.log(screen);//Screen{...}
窗口尺寸
screen.width - 屏幕寬度
screen.height - 屏幕高度
screen.availWidth - 可用的屏幕寬度
screen.availHeight - 可用的屏幕高度
console.log(screen.width);//1536
console.log(screen.height);//864
console.log(screen.availWidth);//1536
console.log(screen.availHeight);//824
navigator對(duì)象
window.navigator對(duì)象和上面的screen對(duì)象差不多诱咏,window.navigator 對(duì)象包含有關(guān)訪問者瀏覽器的信息苔可,window.navigator對(duì)象在編寫時(shí)也可以不使用 window 這個(gè)前綴。
console.log(navigator);//Navigator{...}
navigator對(duì)象包含的信息明顯更多一些袋狞,但是navigator對(duì)象的信息具有誤導(dǎo)性焚辅,因?yàn)閚avigator的數(shù)據(jù)可能會(huì)被更改,或者瀏覽器識(shí)別錯(cuò)誤等苟鸯。一些常見的navigator屬性信息:
- navigator.appCodeName - 瀏覽器代號(hào)
- navigator.appName - 瀏覽器名稱
- navigator.appVersion - 瀏覽器版本
- navigator.cookieEnabled - Cookies是否啟用
- navigator.platform - 硬件平臺(tái)
- navigator.userAgent - 用戶代理
- navigator.language - 用戶代理語言
console.log(navigator.appCodeName);//Mozilla
console.log(navigator.appName);//Netscape
console.log(navigator.appVersion);//5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
console.log(navigator.cookieEnabled);//true
console.log(navigator.platform);//win32
console.log(navigator.userAgent);//Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
console.log(navigator.language);//zh-CN
由于 navigator 可誤導(dǎo)瀏覽器檢測(cè)法焰,使用對(duì)象檢測(cè)可用來嗅探不同的瀏覽器,比如window.opera對(duì)象只有Opera瀏覽器才有倔毙。