前兩天接到一個任務:使用者上傳附件後,需要校驗附件中的身分證資訊,若存在錯誤必須把所有錯誤訊息一次展示出來。
我一開始的直覺是用 jQuery UI 的 Dialog,但這個專案既有的做法是 showModalDialog,為了風格統一,只好沿用。以下把用法記錄下來。
window.showModalDialog() 用來建立一個顯示 HTML 內容的模態對話框。因為是對話框,它並不具備一般 window.open() 開出的視窗的所有屬性。
var returnValue = window.showModalDialog(URL [, arguments] [, features]);
URL(必選):對話框要顯示的文件網址。
arguments(可選):用來向對話框傳遞參數,型別不限,包含陣列等複合物件。對話框內部透過 window.dialogArguments 取得傳入的參數。
features(可選):描述對話框外觀,多個設定之間用分號隔開。
dialogHeight 對話框高度,不小於 100px
dialogWidth 對話框寬度
dialogLeft 距離桌面左側的距離
dialogTop 距離桌面上方的距離
center { yes | no | 1 | 0 } 是否居中,預設 yes,仍可指定高寬
help { yes | no | 1 | 0 } 是否顯示說明按鈕,預設 yes
resizable { yes | no | 1 | 0 } 是否可改變大小,預設 no(IE5+)
status { yes | no | 1 | 0 } 是否顯示狀態列
scroll { yes | no | 1 | 0 | on | off } 是否顯示捲軸,預設 yes
var rv = window.showModalDialog( "<%=path%>/query/query_showErrorInfo.action", "", "dialogWidth=600px;dialogHeight=" + height + "px;" + "dialogLeft=400px;dialogTop=200px;center=no;resizable=no");
這裡的 height 是依錯誤筆數動態計算的,讓視窗高度能貼合內容;url 指向一個 action,該 action 負責取得所有錯誤人員資料並轉送到 errorInfo.jsp。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>批量查詢身份錯誤人員名單</title>
<script type="text/javascript" src="/bjlc/js/jquery-1.4.4.min.js"></script>
<link href="/bjlc/css/queryErrorInfo.css" rel="stylesheet" type="text/css" />
<link href="/bjlc/css/index.css" rel="stylesheet" type="text/css" />
</head>
<base target="download">
<body>
<div class="errorMain">
<div class="errorBtn">
<input type="button" value="我要修改" class="button_08" onclick="wyxg();"/>
<input type="button" value="繼續查詢" class="button_08" onclick="jxcx();"/>
</div>
<div id="showErrorInfo">
<table class="errorTable" cellpadding="0" cellspacing="0">
<tr>
<td colspan="3" class="errorTitle">錯誤人員名單</td>
<s:form theme="simple" id="error_download" namespace="/query" method="post"></s:form>
</tr>
<tr>
<td width="20%" align="center">姓名</td>
<td width="30%" align="center">身份證</td>
<td width="50%" align="center">錯誤信息</td>
</tr>
<s:iterator value="#request.ecList">
<tr>
<td><s:property value="xm"/></td>
<td><s:property value="sfz"/></td>
<td><s:property value="message"/></td>
</tr>
</s:iterator>
</table>
</div>
</div>
</body>
<SCRIPT type="text/javascript">
// 依內容高度動態調整
function setHeight(){
var _allH = $(".errorMain").height();
if (_allH >= 536) {
$("#showErrorInfo").css("height", "500px");
}
}
window.onload = setHeight;
</SCRIPT>
</html>
html, body, .errorMain {
overflow: hidden;
height: 100%;
}
#showErrorInfo {
width: 100%;
OVERFLOW-Y: auto;
OVERFLOW-X: hidden;
}
.errorTable {
width: 90%;
margin: 10px 5%;
font-size: 12px;
border: 1px solid #8DC8FF;
}
.errorTable td {
height: 40px;
border-right: 1px solid #8DC8FF;
border-bottom: 1px solid #8DC8FF;
text-align: center;
}
.errorTable td:last-child { border-right: 0px; }
.errorTitle {
font-weight: bold;
font-size: 14px;
background-color: #C6E3FF;
color: #176ED2;
text-align: center;
}
.errorBtn {
width: 100%;
height: 20px;
text-align: center;
bottom: 0px;
position: absolute;
}
實際成果如下:

這個設計有幾個聰明之處:把 dialogHeight 做成動態計算,避免錯誤筆數多寡造成視窗過大或出現雙重捲軸;用 <base target="download"> 讓頁內的下載連結導到隱藏 iframe,兼顧「不跳新視窗」與「能下載」;再用 #showErrorInfo 的 overflow-y:auto 把捲軸集中在表格區,標題與按鈕固定可見。
但必須誠實說:showModalDialog 已被標準棄用,Chrome 與 Firefox 早已移除支援,只在 IE 系與部分舊版套殼瀏覽器可用。新專案請改用 jQuery UI Dialog、Bootstrap Modal,或原生的 <dialog> 元素。若必須維護這類老系統,建議做法是包一層:偵測到不支援時自動 fallback 到 window.open 或自製遮罩層,讓功能不至于整塊死掉。