什么是BOM
BOM:Browser Object Model 是瀏覽器對象模型,瀏覽器對象模型提供了獨立與內容的、可以與瀏覽器窗口進行互動的對象結構,BOM由多個對象構成,其中代表瀏覽器窗口的window對象是BOM的頂層對象,其他對象都是該對象的子對象。
目前主流瀏覽器介紹
IE——11: 國內用得最多的IE瀏覽器,歷來對W3C標準支持差。從IE10開始支持ES6標準;
Sarafi:Apple的Mac系統自帶的基于Webkit內核的瀏覽器,從OS X 10.7 Lion自帶的6.1版本開始支持ES6,目前最新的OS X 10.10 Yosemite自帶的Sarafi版本是8.x,早已支持ES6;
Firefox:Mozilla自己研制的Gecko內核和JavaScript引擎OdinMonkey。早期的Firefox按版本發布,后來終于聰明地學習Chrome的做法進行自升級,時刻保持最新;
移動設備上目前iOS和Android兩大陣營分別主要使用Apple的Safari和Google的Chrome,由于兩者都是Webkit核心,結果HTML5首先在手機上全面普及(桌面絕對是Microsoft拖了后腿),對JavaScript的標準支持也很好,最新版本均支持ES6。
這里為什么沒有說到360瀏覽器、搜狗瀏覽器呢?其實這一類瀏覽器只是在以上列舉出來的瀏覽器的內核基礎上,換了一個包裝,添加了一些個性功能而已,本質上并沒有什么區別。
可以操作的BOM對象
window對象
所有瀏覽器都支持 window 對象。它表示瀏覽器窗口。
所有 JavaScript 全局對象、函數以及變量均自動成為 window 對象的成員。
全局變量是 window 對象的屬性。
全局函數是 window 對象的方法。
甚至 HTML DOM 的 document 也是 window 對象的屬性之一:
window.document.getElementById("header");
與此相同:
document.getElementById("header");
window尺寸
有三種方法能夠確定瀏覽器窗口的尺寸(瀏覽器的視口,不包括工具欄和滾動條)。
對于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:
window.innerHeight - 瀏覽器窗口的內部高度
window.innerWidth - 瀏覽器窗口的內部寬度
對于 Internet Explorer 8、7、6、5:
document.documentElement.clientHeight
document.documentElement.clientWidth
或者
document.body.clientHeight
document.body.clientWidth
實用的 JavaScript 方案(涵蓋所有瀏覽器):
var w=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var h=window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
x=document.getElementById("demo");
x.innerHTML="瀏覽器的內部窗口寬度:" + w + ",高度:" + h + "。"
該例顯示瀏覽器窗口的高度和寬度:(不包括工具欄/滾動條)
除此之外,還有一個outerWidth和outerHeight屬性,可以獲取瀏覽器窗口的整個寬高。
其他操作window方法(不常用)
window.open() - 打開新窗口
window.close() - 關閉當前窗口
window.moveTo() - 移動當前窗口
window.resizeTo() - 調整當前窗口的尺寸
navigator
navigator對象表示瀏覽器的信息,最常用的屬性包括:
navigator.appName:瀏覽器名稱;
navigator.appVersion:瀏覽器版本;
navigator.language:瀏覽器設置的語言;
navigator.platform:操作系統類型;
navigator.userAgent:瀏覽器設定的User-Agent字符串。
window.navigator 對象在編寫時可不使用 window 這個前綴。
示例:
txt = "
Browser CodeName: " + navigator.appCodeName + "
";txt+= "
Browser Name: " + navigator.appName + "
";txt+= "
Browser Version: " + navigator.appVersion + "
";txt+= "
Cookies Enabled: " + navigator.cookieEnabled + "
";txt+= "
Platform: " + navigator.platform + "
";txt+= "
User-agent header: " + navigator.userAgent + "
";txt+= "
User-agent language: " + navigator.systemLanguage + "
";document.getElementById("example").innerHTML=txt;
注意
來自 navigator 對象的信息具有誤導性,不應該被用于檢測瀏覽器版本,這是因為:
navigator 數據可被瀏覽器使用者更改
瀏覽器無法報告晚于瀏覽器發布的新操作系統
screen
screen對象表示屏幕的信息,常用的屬性有:
screen.width:屏幕寬度,以像素為單位;
screen.availWidth:屏幕的可用寬度,以像素為單位
screen.height:屏幕高度,以像素為單位;
screen.availHeight:屏幕的可用高度,以像素為單位
screen.colorDepth:返回顏色位數,如8、16、24。
window.screen 對象在編寫時可以不使用 window 這個前綴。
document.write( "屏幕寬度:"+screen.width+"px
" );
document.write( "屏幕高度:"+screen.height+"px
" );
document.write( "屏幕可用寬度:"+screen.availWidth+"px
" );
document.write( "屏幕可用高度:"+screen.availHeight+"px" );
Location
location對象表示當前頁面的URL信息。例如,一個完整的URL:
http://www.example.com:8080/path/index.html?a=1&b=2#TOP
可以用location.href獲取:
document.write(location.href);
要獲得URL各個部分的值,可以這么寫:
location.protocol; // 'http'
location.host; // 'www.example.com'
location.port; // '8080'
location.pathname; // '/path/index.html'
location.search; // '?a=1&b=2'
location.hash; // 'TOP'
要加載一個新頁面,可以調用location.assign()。如果要重新加載當前頁面,調用location.reload()方法非常方便。
示例:加載一個新頁面
function newDoc()
{
window.location.assign("http://www.w3school.com.cn")
}
History
history對象保存了瀏覽器的歷史記錄,JavaScript可以調用history對象的back()或forward (),相當于用戶點擊了瀏覽器的“后退”或“前進”按鈕。
History Back
history.back() 方法加載歷史列表中的前一個 URL。
這與在瀏覽器中點擊后退按鈕是相同的:
function goBack()
?{
?window.history.back()
?}
History Forward
history forward() 方法加載歷史列表中的下一個 URL。
這與在瀏覽器中點擊前進按鈕是相同的:
function goForward()
?{
?window.history.forward()
?}
注意
這個對象屬于歷史遺留對象,對于現代Web頁面來說,由于大量使用AJAX和頁面交互,簡單粗暴地調用history.back()可能會讓用戶感到非常憤怒。
新手開始設計Web頁面時喜歡在登錄頁登錄成功時調用history.back(),試圖回到登錄前的頁面。這是一種錯誤的方法。
任何情況,你都不應該使用history這個對象了。
拓展
系統對話框
alert()警告框,沒有返回值
confirm('提問的內容')返回boolean
prompt(),輸入框,返回字符串或null
window對象常用事件
onload:當頁面加載時
onscroll:當頁面滾動時
onresize:頁面重新定義大小時