瀏覽器對象模型是以window對象為依托的钾菊,window在ES中是全局對象帅矗,因而所有的全局變量和函數(shù)都是它的屬性,并且所有的原生的構(gòu)造函數(shù)也都在它的命名空間下煞烫。
1. window對象:
A. BOM的核心對象浑此;
同時(shí)扮演著全局對象的角色。
全局變量與window對象上定義的屬性區(qū)別:全局變量不能通過delete刪除
var age=29;
window.color="red";
delete window.age;
delete window.color;
console.log(age);//29
console.log(window.color); //undefined
B. 窗口位置
a.窗口相對于屏幕左邊和上邊的位置:
screenLeft, screenTop; //IE Safari Opera Chrome
screenX, screenY; //Safari Chrome Firefox
var leftPos=(typeof window.screenLeft == "number")? window.screenLeft : window.screenX;
var topPos=(typeof window.screenTop == "number")? window.screenTop : window.screenY;
b.窗口精確地移動(dòng)到某個(gè)位置:
window.moveBy(0,100) //移動(dòng)了
window.moveTo(0,100) //移動(dòng)到
C. 窗口大兄拖辍:
a.跨瀏覽器確定窗口大小不是一件簡單的事情凛俱,因?yàn)闉g覽器的原因,但是可以確定頁面視口的大幸鹣堋(去掉邊框):
var pageWidth=window.innerWidth;
var pageHight=window.innerHeight;
if(typeof pageWidth != "number"){
if(document.compatMode == "CSS1Compat"){ //標(biāo)準(zhǔn)模式
pageWidth = document.documentElement.clientWidth;
pageHeight = document.documentElement.clientHeight;
}
else{ //混雜模式
pageWidth = document.body.clientWidth;
pageHeight = document.body.clientHeight;
}
}
b.調(diào)整窗口大小:
window.resizeTo(100,100);
window.resizeBy(100,100);
D. 導(dǎo)航和打開窗口:
window.open() //打開一個(gè)新的瀏覽器窗口
E. 間歇調(diào)用setInterval和超時(shí)調(diào)用setTimeout
a.超時(shí)調(diào)用:每個(gè)超時(shí)調(diào)用都有一個(gè)唯一的數(shù)值ID瘦棋,可以通過該ID來取消超時(shí)調(diào)用稀火。
var timeoutId = setTimeout(function(){
alert("hello");
},1000);
取消clearTimeout(timeoutId)//在指定的時(shí)間尚未過去之前取消超時(shí)調(diào)用,會導(dǎo)致setTimeout里面的function代碼沒有執(zhí)行赌朋。
b.間歇調(diào)用:直到間歇調(diào)用被取消或者頁面卸載才會停止凰狞。該方法也有其ID。
var num =0;
var max = 10;
var intervalId = null;
function incrementNumber(){
num++;
if(num == max){
clearInterval(intervalId);
}
else{
console.log(num); //1-9
}
}
intervalId = setInterval(incrementNumber,1000)
很少真正使用間歇調(diào)用沛慢,可以使用超時(shí)調(diào)用來模擬間歇調(diào)用赡若,間歇調(diào)用缺點(diǎn):
1.后一個(gè)間歇調(diào)用有可能會在前一個(gè)間歇調(diào)用結(jié)束之前啟動(dòng);
2.間歇調(diào)用需要追蹤其ID团甲,以便于取消間歇調(diào)用逾冬。
var num =0;
var max = 10;
var intervalId = null;
function incrementNumber(){
num++;
//如果執(zhí)行次數(shù)未達(dá)到設(shè)定的值,則設(shè)置另一次超時(shí)調(diào)用
if(num < max){
console.log(num);
setTimeout(incrementNumber,1000);
}
}
intervalId = setTimeout(incrementNumber,1000);
F. 系統(tǒng)對話框:
1.alert() (用戶無法控制的對話框躺苦,警告作用)
2.confirm() (用戶可以選擇是否執(zhí)行操作)
if(confirm("你確定進(jìn)入下一頁嗎身腻?")){
alert("歡迎")
}
else{
alert("再見")
}
3.prompt() (用戶可以輸入文本)
var result=prompt("你叫什么名字?","");
if(result != null){
alert("歡迎匹厘,"+result);
}
2. location對象
a. location對象提供了當(dāng)前頁面的與文檔有關(guān)的信息嘀趟。它既是window的屬性,又是document的屬性愈诚,即window.location==document.location
.
b. 作用:解析Url她按,將Url解析為獨(dú)立的片段】蝗幔可以通過location的不同屬性訪問url中的不同信息(hash host hostname href pathname port protocol search)
c.獲取url中的參數(shù):
function getQueryStringArgs() {
var qs=location.search.length>0? location.search.substring(1):"";
var args={};
var items=qs.length ? qs.split("&"):[];
var item=null,name=null,value=null;
for(var i=0;i<items.length;i++){
item=items[i].split("=");
name=decodeURIComponent(item[0]);
value=decodeURIComponent(item[1]);
if(name.length){
args[name]=value;
}
}
return args;
}
d. 位置操作:
打開新的URL地址并且在瀏覽器的歷史記錄中生成記錄(可后退)
location.
window.location="http://baidu.com"
location.assign("http://baidu.com")
打開新的URL并且不生成記錄(不可后退)
location.replace("http://baidu.com")
重新加載頁面:reload
location.reload() //有可能加載瀏覽器緩存頁面
location.reload(true) //重新從服務(wù)器進(jìn)行請求
3. navigator對象
主要作用:識別客戶端瀏覽器
a. 檢測插件:plugins數(shù)組中包含插件的各種信息酌泰。
//非ie瀏覽器有效,因?yàn)閚avigator是NS開發(fā)的匕累,IE不支持宫莱。
function hasPlugin(name) {
name=name.toLowerCase();
for(var i=0;i<navigator.plugins.length;i++){
if(navigator.plugins[i].name.toLowerCase().indexOf(name)>-1){
return true;
}
}
return false;
}
console.log(hasPlugin("flash"));
ie瀏覽器需要利用COM對象來進(jìn)行檢測。
4. screen對象
主要用于表示瀏覽器窗口信息哩罪。比如調(diào)整窗口大小時(shí):
window.resizeTo(screen.availWidth,screen.availHeight)
5. history對象:
保存上網(wǎng)的歷史記錄授霸。
作用:創(chuàng)建前進(jìn)后退按鈕巡验,以及檢測當(dāng)前頁面是否是用戶打開的第一個(gè)頁面。
常見用法:
history.go(1);
history.go(-1);
history.forward();
history.back();
history.go("wrox.com");
if(history.length==0){
console.log("用戶訪問的第一個(gè)頁面")
}