標籤:網站 new 一個 包括 write 模型 後退 w3c 載入
JavaScript BOM對象JavaScript Window - 瀏覽器物件模型
瀏覽器物件模型(BOM)使JavaScript有能力與瀏覽器"對話"。
瀏覽器物件模型(BOM)
瀏覽器物件模型(Browser Object Model:BOM)尚無正式標準,由於現代瀏覽器已經幾乎實現了JavaScript互動性方面的相同方法和屬性,因此常被認為是BOM的方法和屬性。
Window對象
所有瀏覽器都支援window對象,它表示瀏覽器視窗。所有JavaScript全域對象、函數以及變數均自動稱為window對象的成員。全域變數是window對象的屬性,全域函數是window對象的方法,甚至HTML DOM的document也是window對象的屬性之一:
window.document.getElementById("header");
與此相同:
document.getElementById("header");
Window尺寸
有三種方法能夠確定瀏覽器視窗的尺寸,對於IE、Chrome、FireFox、Opera語句Safari:
- window.innerHeight:瀏覽器視窗的內部高度(包括捲軸)
- window.innerWidth:瀏覽器視窗的內部寬度(包括捲軸)
對於IE8、7、6、5:
- document.documentElement.clientHeight
- document.documentElemetn.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;
其他Window方法
window.open() - 開啟新視窗window.close() - 關閉當前視窗window.moveTo() - 移動當前視窗window.resizeTo() - 調整當前視窗的尺寸
JavaScript Window Screen
widow.screen對象包含有關使用者螢幕的資訊。
Window Screen
window.screen對象在編寫時可以不使用window這個首碼,其屬性有:
- screen.availWidth:可用的螢幕寬度
- screen.availHeight:可用的螢幕高度
Window Screen 可用寬度
screen.availWidth屬性返回訪問者螢幕的寬度,以像素計,減去介面特性,比如視窗工作列:
<script>document.write("可用寬度: " + screen.availWidth); // 返回螢幕可用寬度:1920</script>
Window Screen 可用高度
scree.availHeight屬性返回訪問者螢幕的高度,以像素計,減去介面特性,比如視窗工作列:
<script>document.write("可用高度: " + screen.availHeight); // 返回螢幕可用高度:1040</script>
JavaScript Window Location
window.location對象用於擷取當前頁面的地址(URL),並把瀏覽器重新導向到新的頁面。
Window Location
window.location對象在編寫時可不使用window這個首碼,如下例子:
- location.hostname:返回web主機的網域名稱;
- location.pathname:返回當前頁面的路徑和檔案名稱;
- location.port:返回web主機的連接埠(80或443);
- location.protocol:返回所使用web協議(http://或https://);
Window Location Href
location.href屬性返回當前頁面的URL:
<script>document.write(location.href); // 返回當前頁面的整個URL:https://www.baidu.com/</script>
Window Location Pathname
location.pathname屬性返回URL的路徑名:
<script>document.write(location.pathname); // 返回當前URL的路徑名:/</script>
Window Location Assign
location.assign()方法載入新的文檔:
<html><head><script>function newDoc() { window.location.assign("http://www.w3cschool.cc"); // 載入一個新的文檔 }</script></head><body><input type="button" value="Load new document" onclick="newDoc()"></body></html>
JavaScript Window History
window.history對象包含瀏覽器的曆史。
Window History
window.history對象在編寫時可不使用window這個首碼,為了保護使用者隱私,對JavaScript訪問該對象的方法做出了限制:
- history.back():與在瀏覽器中點擊後退按鈕相同;
- history.forward():與在瀏覽器中點擊向前按鈕相同;
Window History Back
history.back()方法載入曆史列表中的前一個URL,這與在瀏覽器中點擊後退按鈕是相同的:
<html><head><script>function goBack() { window.history.back(); // 在頁面上建立後退按鈕 }</script></head><body><input type="button" value="Back" onclick="goBack()"></body></html>
Window History Forward
history.forward()方法載入曆史列表中的下一個URL,這與在瀏覽器中點擊前進按鈕是相同的:
<html><head><script>function goForward() { window.history.forward(); // 在頁面中建立一個向前的按鈕 }</script></head><body><input type="button" value="Forward" onclick="goForward()"></body></html>
JavaScript Window Navigator
window.navigator對象包含有關訪問者瀏覽器的資訊。
Window Navigator
window.navigator對象在編寫時可不使用window這個首碼:
<div id="example"></div><script>txt = "<p>瀏覽器代號: " + navigator.appCodeName + "</p>";txt+= "<p>瀏覽器名稱: " + navigator.appName + "</p>";txt+= "<p>瀏覽器版本: " + navigator.appVersion + "</p>";txt+= "<p>啟用Cookies: " + navigator.cookieEnabled + "</p>";txt+= "<p>硬體平台: " + navigator.platform + "</p>";txt+= "<p>使用者代理程式: " + navigator.userAgent + "</p>";txt+= "<p>使用者代理程式語言: " + navigator.systemLanguage + "</p>";document.getElementById("example").innerHTML=txt;</script>
警告
來自navigator對象的資訊有誤導性,不應該被用於檢測瀏覽器版本,這是因為:
- navigator資料可以被瀏覽器使用者更改;
- 一些瀏覽器對測試網站會識別錯誤;
- 瀏覽器無法報告晚於瀏覽器發布的新作業系統;
瀏覽器檢測
由於navigator可誤導瀏覽器檢測,使用對象檢測可用來嗅探不同的瀏覽器。由於不同的瀏覽器支援不同的對象,我們可以使用對象來檢測瀏覽器。例如:由於只有Opera支援屬性"window.opera",可以據此識別出Opera。
if (window.opera) {...some action...}
JavaScript 彈窗
前端基礎:JavaScript BOM對象