javascript學習(三)——常用方法(2)

來源:互聯網
上載者:User

一、相容性較高的瀏覽器頁面關閉

//關閉網頁,不支援Firefox(Firefox返回上次瀏覽頁面)  
//FireFox非window.open()等彈出頁面,需要在地址欄中輸入about:config, 然後將dom.allow_script_to_close_windows改為true才能達到想要的效果。  
function CloseWebPage() { 
    if (navigator.userAgent.indexOf("MSIE") > 0) { 
        if (navigator.userAgent.indexOf("MSIE 6.0") > 0) { 
            window.opener = null; window.close(); 
        } 
        else { 
            window.open('', '_top'); window.top.close(); 
        } 
    } 
    else if (navigator.userAgent.indexOf("Firefox") > 0) { 
        window.history.go(-1); 
    } 
    else { 
        window.close(); 
    } 

//關閉網頁,不支援Firefox(Firefox返回上次瀏覽頁面)
//FireFox非window.open()等彈出頁面,需要在地址欄中輸入about:config, 然後將dom.allow_script_to_close_windows改為true才能達到想要的效果。
function CloseWebPage() {
    if (navigator.userAgent.indexOf("MSIE") > 0) {
        if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
            window.opener = null; window.close();
        }
        else {
            window.open('', '_top'); window.top.close();
        }
    }
    else if (navigator.userAgent.indexOf("Firefox") > 0) {
        window.history.go(-1);
    }
    else {
        window.close();
    }
}
 

二、window.showModalDialog()擴充

<span style="font-size:13px;">// 開啟ModalDialog子視窗,並擷取傳回值  
function ModalDialogOpen(wUrl, wWidth, wHeight) { 
    if (window.showModalDialog != null)//IE判斷  
    { 
        var returnvalue = window.showModalDialog(wUrl, "_self", "dialogWidth:" + wWidth + "px;dialogHeight:" + wHeight + "px;status:no;help:no;scrolling=yes;scrollbars=yes;center=yes"); 
        if(!returnvalue){ 
            returnvalue = window.ReturnValue;; 
        } 
        return returnvalue; 
    } 
    else { 
        this.returnAction = function(strResult) { 
            if (strResult != null) 
                return strResult; 
        } 
        window.open(wUrl, "", "width=" + wWidth + ",height=" + wHeight + ",menubar=no,toolbar=no,location=no,scrollbars=yes,status=no,modal=yes"); 
    } 

// 關閉ModalDialog子視窗,並傳回值  
function ModalDialogClose(val) { 
    if (window.showModalDialog != null)//IE判斷  
    { 
        if (navigator.userAgent.indexOf("Chrome") > 0) { 
            // Chrome支援  
            window.opener.ReturnValue = val; 
        } else { 
            parent.window.returnValue = val; 
        } 
        window.close(); //firefox不支援  
    } 
    else { 
        window.opener.returnAction(val); 
        top.close(); //IE和FireFox都支援  
    } 

</span> 
<span style="font-size:13px;">// 開啟ModalDialog子視窗,並擷取傳回值
function ModalDialogOpen(wUrl, wWidth, wHeight) {
    if (window.showModalDialog != null)//IE判斷
    {
        var returnvalue = window.showModalDialog(wUrl, "_self", "dialogWidth:" + wWidth + "px;dialogHeight:" + wHeight + "px;status:no;help:no;scrolling=yes;scrollbars=yes;center=yes");
        if(!returnvalue){
            returnvalue = window.ReturnValue;;
        }
        return returnvalue;
    }
    else {
        this.returnAction = function(strResult) {
            if (strResult != null)
                return strResult;
        }
        window.open(wUrl, "", "width=" + wWidth + ",height=" + wHeight + ",menubar=no,toolbar=no,location=no,scrollbars=yes,status=no,modal=yes");
    }
}
// 關閉ModalDialog子視窗,並傳回值
function ModalDialogClose(val) {
    if (window.showModalDialog != null)//IE判斷
    {
        if (navigator.userAgent.indexOf("Chrome") > 0) {
            // Chrome支援
            window.opener.ReturnValue = val;
        } else {
            parent.window.returnValue = val;
        }
        window.close(); //firefox不支援
    }
    else {
        window.opener.returnAction(val);
        top.close(); //IE和FireFox都支援
    }
}
</span>

 

摘自 塗劍凱的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.