DIV CSS 佈局教程網

取得父視窗的 getParent 函式,以及各種開啟視窗的封裝
編輯:JavaScript基礎知識     

以下這組工具函式示範了如何把「開啟視窗」這件事封裝成統一介面,同時處理模態/非模態兩種模式下父子視窗的溝通。

一、模態視窗的封裝

// 開啟模態視窗
function dialog(obj){
    if(obj.url == undefined || obj.url == null){
        throw new Error("please set obj.url");
    }
    var url = obj.url;
    if(url.indexOf("?") != -1){
        url = url + "&r_=" + Math.random();
    } else {
        url = url + "?r_=" + Math.random();
    }

    var w = obj.width  ? parseInt(obj.width)  : screen.width / 2;
    var h = obj.height ? parseInt(obj.height) : 500;
    var t = obj.top    ? parseInt(obj.top)    : 50;
    var l = obj.left   ? parseInt(obj.left)   : (screen.width - parseInt(w)) / 2;

    w = w + "px";  h = h + "px";
    l = l + "px";  t = t + "px";

    var paramStr = "";
    paramStr += ("dialogHeight=" + h + ";");
    paramStr += ("dialogWidth=" + w + ";");
    if(obj.center != undefined && obj.center != null){
        paramStr += "center=" + obj.center + ";";
    } else {
        paramStr += ("dialogTop=" + t + ";");
        paramStr += ("dialogLeft=" + l + ";");
    }
    paramStr += "resizable=yes;scroll=yes";

    var rv = window.showModalDialog(url, window, paramStr);
    if (rv == undefined) { rv = window.returnValue; }
    if(obj.cb){ return obj.cb.call(rv, rv); }
    return rv;
}

function openWin(u, w, h){
    return dialog({ url: u, width: w, height: h });
}

function openWinWithCallBack(u, w, h, fun){
    return dialog({ url: u, width: w, height: h, cb: fun });
}

幾個設計細節值得留意:url 上會自動附加隨機參數避免快取;未指定 left 時以螢幕寬度自動置中;obj.cb.call(rv, rv) 這個寫法把回傳值同時當成回呼的 this 與參數傳入。

二、非模態視窗的封裝

function openWindow(obj){
    if(obj.url == undefined || obj.url == null){
        throw new Error("please set obj.url");
    }
    var url = obj.url;
    url += (url.indexOf("?") != -1 ? "&" : "?") + "r_=" + Math.random();

    var w = obj.width  ? parseInt(obj.width)  : screen.width / 2;
    var h = obj.height ? parseInt(obj.height) : 500;
    var t = obj.top    ? parseInt(obj.top)    : 50;
    var l = obj.left   ? parseInt(obj.left)   : (screen.width - parseInt(w)) / 2;

    // 視窗句柄
    var name = obj.name || ("win_" + new Date().getTime());
    // 是否可以改變視窗大小
    var resizable = obj.resizable || "no";
    // 是否有捲動軸
    var scrollbars = obj.scrollbars || "yes";
    // 是否有狀態列
    var status  = obj.status  || "no";
    // 是否有選單欄
    var menubar = obj.menubar || "no";
    // 是否有工具欄
    var toolbar = obj.toolbar || "no";
    // 是否有網址列
    var locations = obj.locations || "yes";

    return window.open(url, name,
        "height=" + h + ",width=" + w + ",top=" + t + ",left=" + l +
        ",toolbar=" + toolbar + ",menubar=" + menubar +
        ",scrollbars=" + scrollbars + ",resizable=" + resizable +
        ",location=" + locations + ",status=" + status + ",hotkeys=esc");
}

用 new Date().getTime() 產生唯一視窗名稱,能確保每次都開新視窗而不重複使用舊的。

三、子頁面取得父視窗

// 模態視窗開啟模式下,子頁面取得父頁面物件
function getParent(){
    var p = "";
    if (window.opener != undefined) {
        p = window.opener;         // window.open() 開啟的
    } else {
        p = window.dialogArguments; // showModalDialog() 開啟的
    }
    return p;
}

// 子頁面設定 returnValue
function setReturnValue(v){
    if (window.opener != undefined) {
        window.opener.returnValue = v;
    } else {
        window.returnValue = v;
    }
}

這兩個函式的重點是:兩種開啟方式的父子參照名稱不同——window.open 用 opener,showModalDialog 用 dialogArguments。做相容時必須兩者都檢查。

四、滑動門選單

function ScrollDoor(){
    this.value = 0;
}

ScrollDoor.prototype = {
    onlyMenu: function(menus, openClass, closeClass){   // 只有選單,沒有內容
        var _this = this;
        for(var i = 0; i < menus.length; i++){
            _this.$(menus[i]).flag = ++this.value;
            _this.$(menus[i]).value = i;
            _this.$(menus[i]).onclick = function(){
                for(var j = 0; j < menus.length; j++){
                    _this.$(menus[j]).className = closeClass;
                }
                _this.$(menus[this.value]).className = openClass;
            }
        }
    },

    sd: function(menus, divs, openClass, closeClass){    // 兩段 class
        var _this = this;
        if(menus.length != divs.length){
            alert("選單層數量和內容層數量不一樣!");
            return false;
        }
        for(var i = 0; i < menus.length; i++){
            _this.$(menus[i]).flag = ++this.value;
            _this.$(menus[i]).value = i;
            _this.$(menus[i]).onclick = function(){
                for(var j = 0; j < menus.length; j++){
                    _this.$(menus[j]).className = closeClass;
                    _this.$(divs[j]).style.display = "none";
                }
                _this.$(menus[this.value]).className = openClass;
                _this.$(divs[this.value]).style.display = "block";
            }
        }
    },

    sd3class: function(menus, divs, openClass, closeClass, middleClass){  // 三段 class
        var _this = this;
        for(var x = 0; x < menus.length; x++){
            _this.$(menus[x]).state = _this.$(menus[x]).className == openClass ? "open" : "close";
        }
        if(menus.length != divs.length){
            alert("選單層數量和內容層數量不一樣!");
            return false;
        }
        for(var i = 0; i < menus.length; i++){
            _this.$(menus[i]).flag = ++this.value;
            _this.$(menus[i]).value = i;
            _this.$(menus[i]).onclick = function(){
                for(var j = 0; j < menus.length; j++){
                    _this.$(menus[j]).className = closeClass;
                    _this.$(divs[j]).style.display = "none";
                    _this.$(menus[j]).state = "close";
                }
                this.state = "open";
                _this.$(menus[this.value]).className = openClass;
                _this.$(divs[this.value]).style.display = "block";
            }
            _this.$(menus[i]).onmouseover = function(){
                for(var j = 0; j < menus.length; j++){
                    if(_this.$(menus[j]).state != "open"){
                        _this.$(menus[j]).className = closeClass;
                        _this.$(menus[j]).state = "close";
                    }
                }
                if(this.state != "open"){ this.className = middleClass; }
            }
            _this.$(menus[i]).onmouseout = function(){
                if(this.state != "open"){ this.className = closeClass; }
            }
        }
    },

    $: function(oid){
        if(typeof(oid) == "string") return document.getElementById(oid);
        return oid;
    }
}

這個小元件示範了用原型方法組織多個相關操作的寫法,也示範了 var _this = this 這個在閉包中保留正確 this 的經典技巧。

這些程式碼在今天怎麼寫

showModalDialog 已被淘汰,Chrome 與 Firefox 早已移除,不該再用於新專案;其中的 returnValue 溝通模式也一併作廢。要達到「開啟視窗並取得使用者回傳值」,標準做法是 <dialog> 搭配 showModal(),或用 Promise 包裝自製的模態元件。

視窗上的介面參數(toolbar、status、menubar 等)如前面所述,現代瀏覽器一律忽略。而滑動門這類選單,用 CSS 的 :hover、checkbox hack 或幾行原生 JS 切換 hidden 屬性就能完成,不必再手寫物件導向的元件結構。

保留這份紀錄的價值在於:它完整呈現了一個年代裡前端工程師必須自行解決的所有問題——父子視窗通訊、跨瀏覽器差異、無框架的元件組織。這些問題的形式改變了,本質仍在。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved