瀏覽器物件模型(BOM)提供了大量操作視窗、歷史紀錄與文件資訊的介面。本文將常見的 BOM 方法與屬性做個整理,方便日常查閱。
setTimeout() // 延時器,僅執行一次程式碼
clearTimeout() // 清除延時器
setInterval() // 定時器,依指定間隔重複執行
clearInterval() // 清除定時器
window.history.length // 造訪過的歷史頁面數量
window.history.forward() // 前往下一頁
window.history.back() // 返回上一頁
window.history.go(-1) // 跳轉至曾造訪的頁面,負值代表往回
document.referrer // 需將兩個檔案放在伺服器上才能取得正確結果,若直接開啟本機檔案則回傳空字串
window.moveBy // by 為相對位移(相對於目前位置)
window.moveTo // to 為絕對定位(相對於整體畫面)
window.resizeBy // 依指定寬高增加或縮小視窗
window.resizeTo // 將視窗調整為指定的寬與高
window.screenLeft / window.screenX // 與螢幕左側的距離
window.screenTop / window.screenY // 與螢幕頂部的距離
註:IE、Chrome、Safari、Opera 支援 window.screenLeft 與 window.screenTop,但 Firefox 不支援這兩個屬性;Firefox、Chrome、Safari、Opera 支援 window.screenX 與 window.screenY,但 IE 不支援這兩個屬性。
document.body.offsetHeight / document.body.clientHeight // 內容高度(body 高度)
window.innerHeight // Chrome 下使用
document.documentElement.clientHeight // 瀏覽器可視高度
document.documentElement.scrollHeight // 內容高度(HTML 高度)
document.documentElement.scrollTop // 捲軸捲動的高度
window.open // 開啟新視窗
window.close // 關閉視窗
window.defaultStatus / window.status // 狀態列資訊
註:window.status 取得的是腳本賦予的臨時值,window.defaultStatus 取得的是預設值。
document.lastModified // 回傳文件最後修改時間
document.referrer // 上一個瀏覽位置的 URL
document.title // 回傳 title 標籤內的文字
document.URL // 回傳目前頁面的 URL
document.anchors[0].innerHTML // 頁面內第一個錨點的內容
location.reload(false) // 強制重新整理目前頁面
navigator.appCodeName // 瀏覽器的代碼名
navigator.appVersion // 瀏覽器平台與版本資訊(唯讀)
eval // 可處理由字串表示的 JS 程式碼
encodeURI // 編碼
decodeURI // 解碼
以上就是 BOM 常見方法與屬性的整理,希望能對大家的開發有所幫助。