1. 程式人生 > >[Javascript 高階程式設計]學習心得記錄11 js的BOM

[Javascript 高階程式設計]學習心得記錄11 js的BOM

    BOM(瀏覽器物件模型)是web中js的核心,而BOM的核心物件就是window物件。在瀏覽器中,window物件有雙重角色,它既是通過js訪問瀏覽器的一個介面,又是規定的Global物件。此外,還有location等物件,因為整體比較簡單,下面說一些值得注意的東西,普通的我就直接粘程式碼略過了。

一,window物件

1.全域性作用域

    前面說物件的時候寫過Global物件了,在全域性作用域中宣告的變數和函式都會變成window物件的屬性和方法。不過,有兩件值得注意的事情:首先,全域性變數不能通過delete操作符刪除,而直接在window物件上的定義的屬性可以。

        var age = 29;
        window.color = "red";
        
        //throws an error in IE < 9, returns false in all other browsers
        delete window.age;

        //throws an error in IE < 9, returns true in all other browsers
        delete window.color;    //returns true
        
        alert(window.age);      //29
        alert(window.color);    //undefined
    另外,嘗試訪問未宣告的變數會丟擲錯誤,但是通過查詢window物件,可以知道某個可能未宣告的變數是否存在。

2.視窗位置和視窗大小

獲取視窗位置相容,移動是moveTo,moveBy

        var leftPos = (typeof window.screenLeft == "number") ? 
                          window.screenLeft : window.screenX;
        var topPos = (typeof window.screenTop == "number") ? 
                          window.screenTop : window.screenY;

        alert("Left: " + leftPos);
        alert("Top: " + topPos);
獲取頁面視口的大小
        var pageWidth = window.innerWidth,
            pageHeight = window.innerHeight;
            
        if (typeof pageWidth != "number"){
            if (document.compatMode == "CSS1Compat"){
                pageWidth = document.documentElement.clientWidth;
                pageHeight = document.documentElement.clientHeight;
            } else {
                pageWidth = document.body.clientWidth;
                pageHeight = document.body.clientHeight;
            }
        }

        alert("Width: " + pageWidth);
        alert("Height: " + pageHeight);
3.導航和開啟視窗

window.open()方法,既可以導航到一個特定的URL,也可以開啟一個新的瀏覽器視窗。這個方法接收4個引數:要載入的URL、視窗目標、一個特性字元和一個表示新頁面是否取代瀏覽器歷史記錄中當前載入頁面的布林值。這個方法我用得不多,沒有細看,貼個教程http://blog.csdn.net/vastskyjoe/article/details/4122104。

4.超時呼叫和間歇呼叫

    超時呼叫:在指定的時間過後執行程式碼;間歇呼叫:每隔指定的時間就執行一次程式碼。

    超時呼叫使用的是window物件的setTimeout()方法,接收兩個引數:要執行的程式碼和以毫秒錶示的時間。其中,第一個引數可以是包含js程式碼的字串(和eval()函式引數一樣),也可以是一個函式。會返回超時呼叫ID,可以傳入clearTimeout()方法,取消超時任務。

        //set the timeout
        var timeoutId = setTimeout(function() {
            alert("Hello world!");
        }, 1000);
        
        //nevermind ?cancel it
        clearTimeout(timeoutId);
    間歇呼叫用法類似,setInterval()方法
        var num = 0;
        var max = 100;
        
        function incrementNumber() {
            num++;
        
            //if the max has not been reached, set another timeout
            if (num < max) {
                setTimeout(incrementNumber, 500);
            } else {
                alert("Done");
            }
        }
        
        setTimeout(incrementNumber, 500);
二,location物件

    window.location和document.location引用的是同一個物件,可以通過下面的物件屬性對URL進行查詢和操作

屬性 描述
hash 設定或返回從井號 (#) 開始的 URL(錨)。
host 設定或返回主機名和當前 URL 的埠號。
設定或返回當前 URL 的主機名。
href 設定或返回完整的 URL。
設定或返回當前 URL 的路徑部分。
port 設定或返回當前 URL 的埠號。
設定或返回當前 URL 的協議。
設定或返回從問號 (?) 開始的 URL(查詢部分)。
        function getQueryStringArgs(){
        
            //get query string without the initial ?
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
            
                //object to hold data
                args = {},
            
                //get individual items
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                
                //used in for loop
                i = 0,
                len = items.length;
            
            //assign each item onto the args object
            for (i=0; i < len; i++){
                item = items[i].split("=");
                name = decodeURIComponent(item[0]);
                value = decodeURIComponent(item[1]);
                
                if (name.length){
                    args[name] = value;
                }
            }
            
            return args;
        }

        //assume query string of ?q=javascript&num=10
        
        var args = getQueryStringArgs();
        
        alert(args["q"]);     //"javascript"
        alert(args["num"]);   //"10"
三、history物件
    history物件儲存著使用者上網的歷史記錄,從視窗被開啟的那一刻算起。使用go()方法可以在歷史記錄間任意跳轉。用得也不多,就不講了。