js物件導向學習筆記之九(BOM 與 DOM 中常用屬性分析),bomdom
一 BOM對象 window 的 相關屬性1. 使用者配置機器設定物件 navigator
navigator.userAgent //該屬性可以查看使用者機器瀏覽器的配置 "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4"
2. 使用者uri訪問地址 location
主 機:host: "www.baidu.com"
主機名稱:hostname: "www.baidu.com"
完整uri :href: "http://www.baidu.com/s?wd=%E5%A6%82%E4%B"
域 名:origin: "http://www.baidu.com"
路 徑:pathname: "/s"
端 口 號:port: ""
協議類型:protocol: "http:"
重新載入:reload: function 無參數 直接調用
重新載入:replace: function 有參數 調用後 不會在history中產生
GET 參數:search: "?wd=aaa&lang=zh_cn&name=lizsi"
//擷取使用者url 上的參數 //直接使用 getParam('name') function getParam(param){ var getarr = (window.location.search.substring(1)).split('&'); var getparam = {}; for(var j in getarr){ var temp = getarr[j].split('='); temp[0] = temp[0].replace(' ','');//去掉變數名中的空格 getparam[temp[0]] = temp[1]; } return getparam[param]; } //對於pathinfo模式的擷取 直接使用getpathinfo(1); 從1開始 function getpathinfo(index){ var getarr = (window.location.pathname.substring(1)).split('/'); return getarr[parseInt(index)-1]; }
3. 曆史訪問記錄 history
只允許訪問本網域名稱下的記錄
後退 一 頁:back: function 無參數 直接調用
前進 一 頁:forward: function 無參數 直接調用
返回指定頁:go: function -1 後退一頁 0 重新整理當前頁 1 前進一頁 2 前進倆頁
pushState: function () 參數
replaceState: function () 參數
//詳情查看 http://www.zhangxinxu.com/study/201306/ajax-page-html5-history-api.html?area=pudong//點擊過的 加入記錄var query = this.href.split("?")[1];history.pushState({ title: title }, title, location.href.split("?")[0] + "?" + query);var title = $(this).text().replace(/\d+$/, "");document.title = title; //監聽 相關事件window.addEventListener("popstate", function() { history.replaceState(null, document.title, location.href.split("?")[0]); });
4. 螢幕對象 screen
螢幕可見高度: availHeight: 667
螢幕可見寬度: availWidth: 375
螢幕解析度: colorDepth: 24
螢幕高度: height: 667
螢幕寬度: width: 375
availLeft: 0
availTop: 0
二 DOM 對象 document1. 緩衝 cookie
//直接使用 cookieParam('name') function cookieParam(param){ var getarr = document.cookie.split(';'); var getparam = {}; for(var j in getarr){ var temp = getarr[j].split('='); temp[0] = temp[0].replace(' ','');//去掉變數名中的空格 getparam[temp[0]] = temp[1]; } return getparam[param]; }