天天看點

javascript中BOM對象(二)

javascript中BOM對象(二)

一、location對象

屬性:

location.hash:在url中#後面的字元串,如果不存在則為空字元串

location.host:伺服器以及端口号:例如:www.wrox.com/80

location.hostname:伺服器名,例如:www.wrox.com

location.href:整個url位址

location.pathname:url中的路徑和檔案名

location.port:端口号

location.protocol:頁面使用的協定

location.search:url中的查詢字元串

location.origin:url中的源位址。www.wrox.com

查詢字元串的封裝函數

let getString = function () {
      let qs = (location.search.length > 0 ? location.search.substring(1) : "");
      let args = {};
      let test = qs.split("&");
      let test_son = test.map(kv =>{
        kv.split("=");
      })
      for(let item of test_son){
        let name = item[0];
        let value = item[1];
        args[name] = value;
      }
      return args;
    }
    
           

URLSearchParams API

這個API可以對location.search所得的資料進行拆解,可以使用has()方法判斷是否具有某個屬性,也可以使用get()來擷取值。

同時這個API也具有疊代接口,是以也可以對其進行疊代。

history.assign()

加載一個新的文檔,可以傳回,裡面穿url位址

location.reload()

對該頁面進行強制重新整理,相當于重新從資料庫中取出資料

** location.replace() **

相當于取代該文檔,不能傳回。

navigator對象

1、appCodename傳回的是浏覽器的代碼名

2、appName傳回的是浏覽器的名稱

3、appVersion 傳回的是浏覽器的平台和版本

4、CookieEnabled傳回的是否使用cookie的布爾值

5、platform 輸出的是運作的作業系統

6、UserAgent 由用戶端向伺服器發送的user-agent的頭部

7、javaEnabled()方法 測試浏覽器是否支援java,傳回布爾值

8、監測浏覽器中是否存在某種插件(IE10以及之前的IE浏覽器需要使用ActiveXObject)

function hasIEPlugin(name) {
      try {
        new ActiveXObject(name);
        return true;
      } catch (ex) {
        return false;
      }
    }
    function hasPlugins(name) {
      name = name.toLowerCase();
      for (let plugin of window.navigator.plugins) {
        if (plugin.name.toLowerCase().indexOf(name) > -1) {
          return true;
        }
      }
      return false;
    }
    // 這裡使用兩種方法來判斷是否存在flash插件
    function hasFlash(){
      var result = hasPlugins("Flash");
      if(result){
        result = hasIEPlugin("ShockwareFlash.ShockwareFlash");
      }
      return result;
    }
    console.log(hasFlash());
           

screen對象

1、availHeight傳回的是可用螢幕的高度 (不包括window工作列)

2、availWidth傳回的是可用螢幕的寬度 (不包括window工作列)

3、colorDepth //屬性傳回目标裝置或緩沖器上的調色闆的比特深度

4、width 螢幕總寬度

history對象

history.pushState():裡面傳入三個參數,第一個是一個對象(表示狀态),第二個是一個标題字元串,第三個是相對的url位址(),并且這裡必須是在開啟的伺服器頁面才會有效,在正常的HTML頁面報錯。

history.replaceState():替換某一個狀态的url

history.popState():将壓入棧的狀态url彈出,相當于按下左邊按鈕。

history.go():裡面可以放數字,整數表示前進幾頁,負數表示後退幾頁

history.back():可以使用history.go(-1)表示

history.forword():可以用history.go(1)表示

history.length:表示該視窗的頁面數

這些方法都是保持頁面不重新整理,對url進行改變。