1. 程式人生 > >BOM 瀏覽器物件模型

BOM 瀏覽器物件模型

1)navigator.userAgent

瀏覽器代理檢測,可以檢測出來使用者使用的瀏覽器型別,也可以檢測瀏覽器所在的作業系統 navigator.userAgent 2)判斷當前瀏覽器的名稱 indexOf 代理檢測 能力檢測

var str = navigator.userAgent;
            if(str.indexOf("Chrome")!=-1){
                alert("谷歌瀏覽器");
            }

3)判斷PC端還是移動端

var arr = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
            var str = navigator.userAgent;
            for(var i = 0; i < arr.length; i++){
                if(str.indexOf(arr[i])!=-1){
                    console.log("移動端");
                    location.href = "mobile.html";
                }
            }

4)location 物件

 4)location  物件      
可讀可寫  能取值能賦值
屬性:hash、host、hostname、href、pathname、port、protocol、search
href:設定或返回完整的 URL。(常用)
hash:設定或返回從井號 (#) 開始的 URL(錨)。
search:設定或返回從問號 (?) 開始的 URL(查詢部分)。(常用)

host	設定或返回主機名和當前 URL 的埠號。
hostname	設定或返回當前 URL 的主機名。
pathname	設定或返回當前 URL 的路徑部分。
port	設定或返回當前 URL 的埠號。
protocol	設定或返回當前 URL 的協議。

方法:assign(url), replace(url) , reload()
assign()	載入新的文件。
reload()	重新載入當前文件。
replace()	用新的文件替換當前文件。

5)history物件 儲存歷史記錄

  • 後退一頁 history.go(-1) history.back()
  • 前進一頁 history.go(1) history.forward()
  • 前進兩頁 history.go(2)
  • 5)window.onload 頁面載入完畢後執