天天看點

Javascript(五)Javascript基礎(浏覽器對象BOM)

在Javascript(一) Javascript與HTML結合使用簡單學習中提到了javascript的語言組成,其中有BOM浏覽器對象模型,要學習javascript語言BOM的學習也就必不可少了。

BOM組成

BOM Window

BOM Navigator

BOM Screen

BOM History

BOM Locatiom

BOM Window

innerheight、innerwidth 與outerheight 、outerwidth

type="text/javascript">

        /**
         * window對象的屬性:
         *      innerheight 傳回視窗的文檔顯示區的高度。不包括工具欄
         *      innerwidth  傳回視窗的文檔顯示區的寬度。不包括滾動條
         *      outerheight 傳回視窗的外部高度。包括工具欄
         *      outerwidth  傳回視窗的外部寬度。包括滾動條
         */
        function init(){
            var innerheight= window.document.innerHeight;
            var innerwidth=window.document.innerWidth;
            alert("高度:" +innerHeight+"\n"+"寬度:"+innerWidth);
            var outheight=window.document.outerHeight;
            var outwidth=window.document.outerWidth;
            alert("高度:" +outheight+"\n"+"寬度:"+outwidth);
        }
        </script>
    <body onload="init()">      

三種對話框

alert("彈出一個alert");
 confirm("這是一個确認框”");
 prompt("輸入框");      

opener與parent差別

opener:代表父視窗,應用opener要求兩個視窗必須有父子關系,

以下2種情況具有父子關系:

1、超連結(設定target屬性為_blank)

2、open方法打開的視窗

open.html頁面

<script type="text/javascript">/**
      * window對象opener與parent
      * 
      * 
      */
     function fun(){
        window.open("open_sub.html");
     }

    </script>
    <body
        <input type="text" name="" id="txt"/>
        <input type="button" value="打開新的頁面" onclick="fun()"
        <a href="open_sub.html" target="_blank">打開新頁面</a>
    </body>      

open_sub.html

function fun(){
            //擷取文本框内容
            var value=document.getElementById("txt").value;
            //擷取父視窗對象
            var w=window.opener;
            //獲得父視窗文本框對象
            var txt=w.document.getElementById("txt");
            //将内容指派給父視窗文本框的value值
            txt.value=value;
        }
    </script>
    <body>
        <input type="text" name="" id="txt" />
        <input type="button" value="傳遞資料給父視窗" onclick="fun()"      
Javascript(五)Javascript基礎(浏覽器對象BOM)

parent:代表父視窗,應用情景有以下2種情況

1、架構

2、内嵌架構

parent.html

<script type="text/javascript">function fun(){
            //擷取目前視窗的文本框内容
            var value=document.getElementById("txt").value;
            //擷取子視窗的對象
            var w=window.frames[0];
            //擷取子視窗中文本框對象
            var txt=w.document.getElementById("txt");
            //給子視窗中文本框指派</script>
    <body>
        <input type="text" name="" id="txt"
        <input type="button" value="傳遞給子視窗" onclick="fun()"
        <iframe src="parent_sub.html"></iframe>
    </body>      

parent_sub.html

<script type="text/javascript">function fun(){
            var value=document.getElementById("txt").value;
            var w=window.parent;
            var txt=w.document.getElementById("txt");
            txt.value=value;
        }

    </script>
    <body>
        <input type="text" name="" id="txt"
        <input type="button" value="傳遞給父視窗" onclick="fun()"
    </body>      
Javascript(五)Javascript基礎(浏覽器對象BOM)

self屬性等價于window

status狀态欄

<script type="text/javascript">function init(){
        self.status="加載完成";
     }
    </script>
    <body onload="init()">
    </body>      

setTimeout()與setInterval()差別

BOM History

history.forward();//前進
history.back();//後退
history.go();//直接到某個曆史頁面      

BOM Location

window.location.href="parent.htmls";
window.location.reload();      

繼續閱讀