(一)、本文闡述關於window對象以及下面所屬屬性值,要知道相關的內容先了解下面幾個名詞:本地對象、內置對象、宿主對象。
本地對象:獨立於宿主環境的ECMAScript實現提供的對象。常見的本地對象有:Object、Function、Array、String、Boolean、Number 、Date、RegExp、Error等。
內置對象:內置對象不需要實例化,主要是Global對象和Math對象;
Global對象:它是ECMAScript中最特別的對象,因為實際上它根本不存在,但大家要清楚,在ECMAScript中,不存在獨立的函數,所有函數都必須是某個對象的方法。類似於isNaN()、parseInt()和parseFloat()方法等,看起來都是函數,而實際上,它們都是Global對象的方法。
Math:主要為數學專用值的對象;
var iMax=Math.max(2,0,6,4,10); //iMax:10 var iMin=Math.min(2,0,6,4,); //iMin:0 Math.abs()方法返回數字的絕對值。 Math.ceil()表示向上舍入 Math.floor()表示向下舍入 Math.round()標准的四舍五入
宿主對象:所有的非本地對象都是宿主對象即友ECMAscript實現的宿主環境提供的對象。所有BOM和DOM對象都是宿主對象。
(二)本文闡述的是宿主對象window。它是JavaScript 層級中的頂層對象,表示瀏覽器窗口。常見的方法有:alert、confirm、prompt、open、close方法。相關的屬性有location、navigator、acreen、location、frameset等。
2.1 location對象
location是BOM中最重要的對象之一,它包含了當前 URL 的信息。
如果url為https://www.baidu.com:8888?a=1&b=2&c=3#contents;
location.hash : "#contents"; location.host: "www.baidu.com:8888";
location.hostname:"www.baidu.com" ; location.port: "8888";
location.protocl: "https"; location.search:"?a=1&b=2&c=3"
location.path:"";等。
常見的獲取url參數的方法:
方法一:
function getParm() { var url = location.search;//獲取url的參數例如:?a=12&b=14 var o = {}; if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { o[strs[i].split("=")[0]]=strs[i].split("=")[1]; } } return o; }
方法二:
getParams(){ var p = location.search.substr(1), reg = /&?([^&=]+)=?([^&]*)/g, o = {}; p.replace(reg, function (match, k, v) { o[k] = v; }); return o; }
2.2、history對象
history對象保存着用戶上網記錄,從窗口被打開的那一刻算起
常用的方法:history.go(-1);返回上一頁。
2.3、navigator(待補充)
2.4screen對象(待補充)