天天看點

第189天:BOM屬性方法

一、BOM---location

1、通路頁面

1 location.href = "http://www.baidu.com";
2 location.assign("http://www.baidu.com");
3 location.replace("http://www.baidu.com");      

replace和assign的差別

 replace()方法所做的操作與assign()方法一樣,

 但它多了一步操作,即從浏覽器的曆史記錄中删除了包含腳本的頁面,

 這樣就不能通過浏覽器的後退按鈕和前進按鈕來通路它了,

assign()方法卻可以通過後退按鈕來通路上個頁面。

2、重新整理頁面

1 //重新整理頁面
2         document.getElementsByTagName('button')[1].onclick = function(){
3         location.reload(true);    //從伺服器重載目前頁面
4         location.reload(false);   //從浏覽器緩存中重載目前頁面
5         location.reload();        //從浏覽器緩存中重載目前頁面
6         }      

3、其他屬性

(1) hash:如果URL中包含有“#”,該方法将傳回該符号之後的内容

 (例如:http://www.itcast.cn/index.html#welcome的hash是“#welcome”)。

(2) host:伺服器的名字,例如www.baidu.com。

(3) hostname:通常等于host,有時會省略前面的www。

(4) href:目前頁面載入的完整URL。

(5) pathname:URL中主機名之後的部分。例如:http://www.leledeng.com/html/js/index.html的pathname是“/html/js/index.html”。

(6) port:URL中聲明的請求端口。預設情況下,大多數URL沒有端口資訊(預設為80端口),是以該屬性通常是空白的。像http://www.leledeng.com:8080/index.html這樣的URL的port屬性為8080。

(7) protocol:URL中使用的協定,即雙斜杠(//)之前的部分。例如http://www.itcast.cn中的protocol屬性是http:

(8) ftp://www.leledeng.com的protocol屬性等于ftp:。

(9)search:執行GET請求的URL中的問号?後的部分,又稱查詢字元串。

// 例如http://www.leledeng.com/search.html?name=lele中的search屬性為?name=lele。

二、BOM---history

1 <body>
 2 <input type=button value=重新整理 onclick="window.location.reload()">
 3 <input type=button value=前進 onclick="window.history.go(1)">
 4 <input type=button value=後退 onclick="window.history.go(-1)">
 5 <input type=button value=前進 onclick="window.history.forward()">
 6 <input type=button value=後退
 7        onclick="window.history.back()">
 8 <input type=button value=後退+重新整理
 9        onclick="window.history.go(-1);window.location.reload()">
10 </body>      

三、BOM---navigator

1 <script>
 2 //    利用userAgent屬性判斷是哪個浏覽器
 3     function CheckBrowser(){
 4         var u_agent = navigator.userAgent;
 5         var browser_name='未知浏覽器';
 6         if(u_agent.indexOf('Firefox')>-1){
 7             browser_name='Firefox';
 8         }else if(u_agent.indexOf('Chrome')>-1){
 9             browser_name='Chrome';
10         }else if(u_agent.indexOf('Trident')>-1&&u_agent.indexOf('rv:11')>-1){
11             browser_name='IE11';
12         }else if(u_agent.indexOf('MSIE')>-1&&u_agent.indexOf('Trident')>-1){
13             browser_name='IE(8-10)';
14         }else if(u_agent.indexOf('MSIE')>-1){
15             browser_name='IE(6-7)';
16         }else if(u_agent.indexOf('Opera')>-1){
17             browser_name='Opera';
18         }else{
19             browser_name+=',info:'+u_agent;
20         }
21         document.write('浏覽器類型為:'+browser_name+'<br>');
22         document.write('userAgent屬性值為:'+u_agent+'<br>');
23     }
24 
25 CheckBrowser();
26 </script>      

四、BOM---document

1 <script>
2     //對象集合屬性
3     document.write("文檔包含:"+document.forms.length+"個表單"+"<br />");
4     //forms[]對象集合統計表單個數
5     document.write(document.all.length+"<br />");//14
6     document.write(document.links[0]);//輸出:http://www.baidu.com/
7 </script>      

繼續閱讀