DIV CSS 佈局教程網

window.open 與 showModalDialog 用法解析
編輯:關於JavaScript     

在傳統網頁開發中,常需要開啟新視窗或彈出對話框。window.open 可開啟一般視窗,showModalDialog 則可建立模態對話框。兩者都能透過參數控制外觀與行為。

window.open 基本用法

基本語法為 window.open(pageURL, name, parameters)。pageURL 是要開啟的頁面,name 是視窗名稱,parameters 則用逗號分隔多個設定。

window.open("page.html", "newwindow", "height=400,width=600,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no");

常見參數包含 height、width、top、left、toolbar、menubar、scrollbars、resizable、location 與 status。yes 或 no 也可用 1 或 0 表示。

showModalDialog 基本用法

showModalDialog 主要見於舊版 IE,用來建立顯示 HTML 內容的模態對話框。基本寫法如下:

var result = window.showModalDialog("modal.html", obj, "dialogWidth=200px;dialogHeight=100px");

它有三個主要參數:sURL 是對話框頁面,vArguments 是傳入值,sFeatures 是外觀設定。常用外觀設定包含 dialogHeight、dialogWidth、dialogLeft、dialogTop、center、resizable、status 與 scroll。

參數傳遞與回傳

傳入參數不限於字串,也可以是物件。對話框中可用 window.dialogArguments 接收;若要回傳結果,則可設定 window.returnValue。

需要注意的是,這類 API 較偏向舊式 IE 環境。現代專案通常會改用 layer、dialog 或自訂彈窗元件。

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