BOM 瀏覽器對象模型_window.navigator

window.navigator 對象html

包含瀏覽器和系統信息的 Navigator 對象。java

經過這個屬性 瞭解用戶的環境信息android

window.navigator.userAgent數組

返回瀏覽器的 User Agent 字符串,表示瀏覽器的廠商和版本信息瀏覽器

用戶能夠改變這個字符串。服務器

這個字符串的格式並沒有統一規定,也沒法保證將來的適用性,各類上網設備層出不窮,難以窮盡。cookie

因此,如今通常再也不經過它識別瀏覽器了,異步

使用「功能識別」方法,即逐一測試當前瀏覽器是否支持要用到的 JavaScript 功能ide

  • 經過 userAgent 能夠大體準確地識別 手機瀏覽器,方法就是測試是否包含 mobi 字符串
  • /mobi/i.test(userAgentStr);
  • /mobi|android|touch|mini/i.test(ua)

window.navigator.plugins函數

返回一個相似數組的對象,成員是 Plugin 實例對象,表示瀏覽器安裝的插件,好比 Flash、ActiveX 等

  • var pluginsLength = navigator.plugins.length;
    
    for (var i = 0; i < pluginsLength; i++) {
        console.log(navigator.plugins[i].name);
        console.log(navigator.plugins[i].filename);
        console.log(navigator.plugins[i].description);
        console.log(navigator.plugins[i].version);
    }

5

window.navigator.platform

返回用戶的操做系統信息,好比MacIntel、Win3二、Linux x86_64等 

  • navigator.platform // "Linux x86_64"

window.navigator.onLine

返回一個布爾值,表示用戶當前在線仍是離線

若是是false,能夠判定用戶必定離線

若是是 true,就不必定真的在線

  • 用戶變成在線會觸發 online 事件,變成離線會觸發 offline 事件
  • window.addEventListener('offline', function(e) { 
        console.log('offline'); 
    });
    window.addEventListener('online', function(e) {
        console.log('online'); 
    });

window.navigator.language

返回一個字符串,表示瀏覽器的首選語言。該屬性只讀

window.navigator.languages

返回一個數組,表示用戶能夠接受的語言

HTTP 請求頭信息的 Accept-Language 字段,就來自這個數組

  • navigator.languages     // ["en-US", "en", "zh-CN", "zh", "zh-TW"]

window.navigator.geolocation

返回一個 Geolocation 對象,包含用戶地理位置的信息

注意,該 API 只有在 HTTPS 協議下可用,不然調用下面方法時會報錯

  • Geolocation 對象提供下面三個方法

window.navigator.geolocation.getCurrentPosition()

獲得用戶的當前位置

window.navigator.geolocation.watchPosition()

監聽用戶位置變化

window.navigator.geolocation.clearWatch()

取消 watchPosition() 指定的監聽函數

  • 注意,調用這三個方法時,瀏覽器會跳出一個對話框,要求用戶給予受權

window.navigator.cookieEnabled

屬性返回一個布爾值,表示瀏覽器的 Cookie 功能是否打開

這個屬性反映的是瀏覽器總的特性,與是否儲存某個具體的網站的 Cookie 無關

用戶能夠設置某個網站不得儲存 Cookie,這時 cookieEnabled 返回的仍是 true

  • window.navigator.javaEnabled()

返回一個布爾值,表示瀏覽器是否能運行 Java Applet 小程

  • window.navigator.sendBeacon()

用於向服務器異步發送數據

window.screen 對象

表示當前窗口所在的屏幕,提供顯示設備的信息

window.screen.width        

瀏覽器窗口所在的屏幕的寬度(單位像素)。

window.screen.height

瀏覽器窗口所在的屏幕的高度(單位像素)

除非調整顯示器的分辨率,不然這個值能夠看做常量,不會發生變化。

顯示器的分辨率與瀏覽器設置無關,縮放網頁並不會改變分辨率。

window.screen.availWidth

瀏覽器窗口可用的屏幕寬度(單位像素)

window.screen.availHeight

瀏覽器窗口可用的屏幕高度(單位像素)

由於部分空間可能不可用,這個屬性等於 height 減去那些被系統組件的高度。

好比系統的任務欄 或者 Mac 系統屏幕底部的 Dock 區

window.screen.pixelDepth

整數,表示屏幕的色彩位數

好比24表示屏幕提供24位色彩

window.screen.colorDepth

整數,表示應用程序的顏色深度

window.screen.orientation

返回一個對象,表示屏幕的方向

  • window.screen.orientation.type 屬性是一個字符串,表示屏幕的具體方向

"landscape-primary"    表示橫放

"landscape-secondary"    表示顛倒的橫放

"portrait-primary"    表示豎放

"portrait-secondary"    表示顛倒的豎放

  • window.screen.orientation    // { angle: 0, type: "landscape-primary", onchange: null }
  • 保證屏幕分辨率大於 1024 x 768
  • if (window.screen.width >= 1024 && window.screen.height >= 768) {
        // 分辨率不低於 1024x768
    }
  • if ((screen.width <= 800) && (screen.height <= 600)) {
        window.location.replace('small.html');
    } else {
        window.location.replace('wide.html');
    }
相關文章
相關標籤/搜索