BOM:瀏覽器對象模型 BOM大部分都是對window對象的操作
window.open
window.open(頁面的地址URL,打開的方式)
- 如果URL默認為空,則默認打開一個新的空白頁
- 如果打開方式為空,則默認為新窗口方式
- open()方法的返回值: 返回新打開的窗口window對象
window.close
關閉窗口(有兼容性問題)
1 firefox 默認無法關閉
2 chorme 默認直接關閉
3 ie 詢問用戶
4 可以在關閉本窗口中通過JS方法打開新的窗口
<input type="button" value="打開新窗口">
<input type="button" value="關閉窗口">
<input type="button" value="關閉新窗口">
對應下面的代碼
var inputall = document.getElementsByTagName("input");
var newPage = null;
inputall[0].onclick = function(){
newPage = window.open("","_blank");
newPage.document.body.style.background = "red"; //不會起作用。除非不是網址而是空白頁的。
}
inputall[2].onclick = function(){
newPage.close(); //新開的
}
window.navigator.userAgent
window.navigator.userAgent ==瀏覽器信息
可以通過這個屬性判斷瀏覽器信息
var string = window.navigator.userAgent;
var word = string.toLowerCase();
window.alert(word);
if(word.indexOf("msie")==-1)
{
window.alert("找不到")
}
window.location
window.location 瀏覽器地址欄信息
window.location 看似一串字符串,其實一個對象
- window.location.href :地址欄的地址url
- window.location.search 這個是url?后面的內容
- window.location.hash: 表示url#后面的內容
文檔寬高及窗口事件
可視區尺寸,沒有border
元素.clientWidth
元素.clientHeight
要是獲取到整個瀏覽器的寬度和高度只有
document.documentElement.clientHeight
或者document.body.clientHeight;
滾動距離
元素.scrollTop/scrollLeft
指的是滾動條距離頂部或者左邊的距離
document.body.scrollTop
document.documentElement.scrollTop
元素.scrollHeight指的是整個里面的高度
兼容性問題
- chorme瀏覽器認為滾動距離是在body上面
- 其他瀏覽器認為滾動距離是在documentElement
建議用下面的方法
var scrollTop = document.documentElement.scrollTop||document.body.scrollTop
onscroll
var i = 0 ;
window.onscroll = function(){
document.title = i++;
}
onresize
onresize:當窗口大小發生改變的時候觸發
var i = 0 ;
window.onresize = function(){
document.title = i++;
}