window
window 物件 it is the global object for client-side JavaScript programs.
JavaScript 程式的執行
  • 第一階段 : script 載入並依順序執行
  • 第二階段 : 進行事件驅動的階段 (非同步的)。
window 列舉

location
window.location === document.location 兩個location指的是同一個物件

可以加上字串在網址列再試試 : ?A=123&b=234&sn=ABCDsdf
window.location 屬性
href http://localhost:7566/WebApp/Learn/JavaScript/Base/window.html?A=123&b=234&sn=ABCDsdf
protocol http:
host localhost:7566
hostname localhost
port 7566
pathname /WebApp/Learn/JavaScript/Base/window.html
search
query string 字串部份 ?A=123&b=234&sn=ABCDsdf
    // 解 query string 值為物件
    function urlArgs() {
        var args = {};
        var query = location.search.substring(1);
        var arr = query.split("&");

        for (var i = 0; i < arr.length; i++) {
            var pos = arr[i].indexOf('=');
            if (pos == -1) continue;
            var name = arr[i].substring(0, pos);
            var value = arr[i].substring(pos + 1);
            args[name] = decodeURIComponent(value);
        }

        return args;
    }
hash
origin http://localhost:7566
window.location 方法
assign 使視窗載入並顯示位於你所指定的URL的文件
replace 使視窗載入並顯示位於你所指定的URL的文件、載入前將目前文件從歷程記錄中移除.
ancestorOrigins [object DOMStringList]
reload 重新載入文件
安全實現跨網域通訊 postMessage

window.postMessage

postMessage 可以安全地實現跨網域傳遞資料或訊息,只要正確使用, 這種方法就很安全。

//來源網頁

        $(document).ready(function () {

            //監聽 message
            window.addEventListener('message', function (event) {
                //限制來源網站為
                if (event.origin.indexOf('ddmg.com.tw') != -1) {
                    
                    console.log(event.data);    //event.data 即為目標網址傳來的資料;

                }
            });
        });

        function openFacebokRegister() {
            window.open('http://core.ddmg.com.tw/Learn/fbLogin.html','', config = 'height=500,width=500');
        }

//目標網頁

        $(document).ready(function () {
            window.opener.postMessage('23k4jh2i3ut4-2k3j423k4h-sdf2342', '*');
            window.close();
        });

history

瀏灠歷程

window.history 屬性
length 14
state null
window.history 方法
back 回上一頁
forward 往前一頁
go function go() {
//至任意數目的頁面 , 負數是往後跳幾頁
}
pushState function pushState() { [native code] }
replaceState function replaceState() { [native code] }
navigator 早期大多用來識別瀏灠器的
screen
Dialog Boxes對話框
                        alert('顯示訊息');

                        if(confirm('是否確認')) { 
                        
                        }

                        var str = prompt('請輸入資料')

                    
onerror
onerror 事件處理器是舊的東西, try catch 出現之後以使用 try catch 較為方便且好用 .


    // Display error messages in a dialog box, but never more than 3
    window.onerror = function (msg, url, line) {
        if (onerror.num++ < onerror.max) {
            alert("ERROR: " + msg + "\n" + url + ":" + line);
            return true;
        }

        // 回傳 false 表示已處理好錯誤 , 不需更進一步的動作
    }
    onerror.max = 3;
    onerror.num = 0;

window 焦點 使用在如果有子視窗時 , 想要讓子視窗一直顯示在最上層 , 或是讓視窗移到背景
  subWin= window.open(xxxxx)   ==>   subWin.focus( ) Or subWin.blur( )

window.blur( )
window.focus( )
設定 / 解除事件

這些 IE 都不支援
像要針對視窗被拖曳 , 按鈕被點選等事件 , 優先進行事件處理時

window.captureEvents : 指定要取得的事件
window.handleEvent    : 處理事件
window.releaseEvent   : 解除事件
window.routeEvent       : 通過事件

開啟 / 關閉視窗
對子視窗或母視窗的查詢狀態或操作
close 動作 : 關閉視窗
closed、open 狀態 : 某個視窗是否關閉 true:表示關閉
    var subWind = window.open   ('url', ....);
    
    //subWind 控制子視窗的所有動作及物件 

    subWind.document.open( ) :
    subWind.document.write("顯示在子視窗的文字內容")
    subWind.document.write("顯示在子視窗的文字內容")
    subWind.document.close( );

    if (subWin.closed) {
        //表示己關閉子視窗
    }
opener window.opener
    //window.opener 表示 open 開啟的子視窗可以用此物件來取得母視窗的window...進一步操作之
快顯視窗 createPopup
只有 IE 有用, 而且新版的好像移掉了
    function openNewPopup() {
        var wObj = window.createPopup();
        var popObj = wObj.document.body;
        popObj.style.border = "solid 2px blue";
        popObj.document.bgColor = "yellow";
        popObj.innerHTML = "快顯視窗裡的內容"
        wObj.show(-100, 80, 400, 320, document.body);
    }
狀態列參照 / 設定

defaultStatus

status
defaultStatus -->預設狀態列的顯示內容
status             -->狀態列的顯示內容

setTimeout
    var setT = window.setTimeout(
                                 function () { },
                                 1000 * 36,
                                 {}
                                 );
    //
    window.clearTimeout(setT);
setInterval
    var it = window.setInterval(
                                function () { },
                                1000 * 36,
                                {}
                               );
    window.clearInterval(it);
Relationships Between Frames
iframe 元素具有一個 contentWindow 屬性, 指的是 iframe 裡的 window物件
    var win = window.open('', ''/*....*/);

    //回指開啟它的視窗
    win.opener

    //來取得iframe , 而不用 getElmentById
    window.frames['id'] 

視窗的高度 / 寬度

更改位置
moveBy 相對
moveTo


更改大小
resizeBy 相對
resizeTo

1.視窗大小          window.outerWidth ; window.outerHeight ;  Netscape的用法 IE不適用


 
IE 用下列的方式
   document.body.clientWidth + 24    
   document.body.clientHeight + 128

   

2.指定視窗大小 resizeTo( w , h )

3.視窗移動到  window.moveTo( x , y )

4.捲動視窗        window.scrollBy ( x , y )
視窗在螢幕上的座標

window. screenLeft

window.screenTop
視窗名

.name


window.navigate( 'http://www.yahoo.com.tw' )
列印
print
window.print

 捲動

scroll
scroll 移動到指定位置
scrollBy 移動到相對位置
scrollTo 移動到指定位置
document.body.scrollLeft IE取得目前捲動的位置
document.body.scrollTop IE取得目前捲動的位置

資訊方塊  --> sidebar (IE不適用)
配合內容調整視窗大小 -- > sizeToContent (IE不適用)
視窗的效果

 

open 傳回值的方式
window.returnValue = "傳回值" ;

直接控制母視窗的傳回物件
window.opener.document.getElementById('objID') = "asdf"

或是在母視窗傳入Argument然後在子視窗控制傳回值

winID = window.open('URL','WindowHeadName','features','replace')

winID.location.href = newURL;

子視窗也可控制父視窗的方法
if (window.opener && ! window.opener.closed){
window.opener.staturs = "改變父視窗的狀態列";
}

父視窗關閉子視窗
if (winID && winID.open && !winID.closed){
winID.closed();
}

window.open(' ' , ' ' , '設定新視窗的特性' )

IE Netscape
left screenX 設定瀏灠程式左上角的X座標
top screenY 設定瀏灠程式左上角的Y座標
height innerHeight 設定瀏覽程式顯示視窗的高,單位為px,最小值為100
width innerWidth 設定瀏覽程式顯示視窗的寬
directories 顯示瀏覽程式連結列
location 網址欄
menubar 功能表
resizeable 調整視窗的尺寸
scrollbars 捲動軸
status 狀態
titlebar 標題列
toolbar 工具列

showModalDialog

在子視窗關閉前
無法操控母視窗
(強制回應)

ReturnValue = showModalDialog( url , sender , parameter )

直接使用參數傳入母視窗的物件來讓子視窗來直接改變或使用
在母視窗呼叫時使用

functionSel_rule( ) {
   var uri= "Dialog.aspx?title=efRuleVariable&link=../rule/Dialog_SelectRuleVar.aspx";
   var nWidth= 450;
   var nHeight= 400;
   var rgParams= {
                                    from: window,
                                    id: document.getElementById("txtRuleValue"),
                                    name: document.getElementById("txtRule")
                                 };
var ReturnValue= window.showModalDialog(uri, rgParams, "dialogHeight:"+nHeight+"px;dialogWidth:"+nWidth+"px;status:no;resizable:yes;help:no;scroll:yes;");
}

在子視窗時使用

var param= window.dialogArguments;

function returnValue() {
if (ruleId + ruleName != "") {
    param.id.value = ruleId;
    param.name.value = ruleName;
    param.descr = ruleDescr;
    closeDialog( );}
}


在母視窗呼叫並且接值的方式
var ReV= window.showModalDialog(xxxx.......)

在子視窗傳回
window.returnValue= 傳回的值

showModelessDialog

子視窗開著時
仍可操控子視窗
(非強制回應)


顯示各種特殊的對話方塊
window.external.ShowBrowserUI(type, null)
type :
LanguageDialog
OrganizeFavorites
PrivacySettings
ProgramAccessAndDefaults





顯示色彩對話方塊 顯示色彩對話方塊
顯示說明文件
按下滑鼠右鍵執行別的視窗 <物件 oncontextmenu="oncontextMenuDiff( ); return false;" >

Window物件更改尺寸和位置的屬性

screenLeft 取得 視窗 左方邊界的座標
screenTop 取得 視窗 上方邊界的座標

Window物件更改尺寸和位置的方法

window . moveTo (x , y) 將瀏覽程式移到 x , y 的座標
window . moveBy (offsetx,offsety) 移動若干參數的位移量
window . resizeTo (width , height) 調整視窗參數
window . resizeBy (offsetX , offsetY) 調整視窗大小 , 放大目前的參數值
參數大於 0 為放大 , 小於 0 為縮小
視窗最大化
window.moveTo(0,0);   //要先將視窗移到0,0 不然會在原地將視窗伸展到最右及最下邊
window.resizeTo(
window.screen.availWidth, window.screen.availHeight );
取得視窗座標
window.screenLeft
window.screenTop

視窗內容的捲動
scroll ( x , y )
scrollTo ( x , y )
scrollBy ( offsetx , offsety )

window.frameElement .frameElement == null 時, 表示本頁不是在框架裡的頁面

.frameElement 可以取得引用的frame物件, ( <iframe iframe src="16_plug-in.html" id="frame1"></iframe > )
如果我是16_plug-in.html 裡使用window.frameElement 即表示我取到了 id="frame1" 的上面這個物件
window.length 表示 內嵌frame的數量
document.embeds  
document.plugins  
frame
frame 物件
框架的分割大小

高 寬
cols 左右分割大小
rows 上下分割大小
height
width
基準位置 parent: 上層框架
self      : 自己這個框架
top       : 最上層框架

呼叫某一層框架的函式 parent . someFunction ( )
框架的狀態

<FRAME SRC="upside.html" NAME="UP" onreadystatechange ="alert(this.readyState)">
更換右側頁框內容 parent . rightFrame . location . href = url
< frameset cols="50% , 50%"  rows="50% , 50%">

  <frame name=" topleft " src=" 12.htm " scrolling= yes | no>

  <frame name=" topright " src=" 34.htm " scrolling= yes | no>

  <frame name=" bottomleft " src=" 56.htm " scrolling= yes | no>

  <frame name=" bottomright " src=" 78.htm " scrolling= yes | no>

  </frameset>
取得各框架的window物件
parent.frames[1].location.href = "90.htm"
parent.topright.lcation.href = "000.htm"
在全視窗顯示HTML文件
如果在框架頁中只使用一個框架載入HTML , 在載入HTML文件時可以強迫瀏覽程式改為全視窗顯示 , 而不是只使用一個框架
if ( window != top ) {top.locatio.href = location.href;}
強迫顯示整份的框架頁
為了不讓使用者只單一載入一個框架 , 所以使用下列方法使之載入的都是整份的框架頁。
if (top . location .href . indexOf ("ch1.htm") == -1){
top . location . href = "ch1.htm?ch2.htm&1"; }