showModalDialog(IE 專用、已棄用)開啟的對話方塊預設會快取頁面內容。當資料更新後再次開啟,顯示的仍是舊資料,因為瀏覽器直接讀取快取而非重新請求伺服器。
在被開啟的**對話方塊頁面** `
` 區塊加入以下 meta 標籤,強制瀏覽器不快取:<head>
<!-- 核心:禁止快取 -->
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache, must-revalidate">
<meta http-equiv="expires" content="0">
<!-- 額外保險:IE 相容 -->
<meta http-equiv="pragma" content="no-cache">
</head>
| Meta 標籤 | 作用 | 備註 |
|---|---|---|
http-equiv="pragma" content="no-cache" | HTTP/1.0 相容,告知瀏覽器不快取 | 最基本設定 |
http-equiv="cache-control" content="no-cache, must-revalidate" | HTTP/1.1 標準,強制重新驗證 | 現代瀏覽器主要依據此項 |
http-equiv="expires" content="0" | 設定過期時間為 0(已過期) | 強制每次請求伺服器 |
Meta 標籤可能被 Proxy 忽略,**伺服器端設定 Response Headers 才是根本解決**:
// WebForms
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Response.Cache.SetExpires(DateTime.MinValue);
Response.AppendHeader("Pragma", "no-cache");
// MVC Controller Action
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult MyDialog() { ... }
// 或在 Action 中手動設定
Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
Response.Headers["Pragma"] = "no-cache";
Response.Headers["Expires"] = "0";
[ResponseCache(NoStore = true, Duration = 0, Location = ResponseCacheLocation.None)]
public IActionResult MyDialog() { ... }
// 或中介軟體全域設定
app.Use(async (context, next) => {
context.Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
context.Response.Headers["Pragma"] = "no-cache";
context.Response.Headers["Expires"] = "0";
await next();
});
// Servlet
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
// Spring MVC
@GetMapping("/dialog")
@ResponseHeaders(
CacheControl = "no-cache, no-store, must-revalidate",
Pragma = "no-cache",
Expires = "0"
)
public String dialog() { ... }
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
app.get('/dialog', (req, res) => {
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
res.render('dialog');
});
若無法修改伺服器端,呼叫端每次請求附加隨機參數強制重新載入:
// 開啟對話方塊時
var url = "dialog.aspx?_t=" + new Date().getTime(); // 或 Math.random()
window.showModalDialog(url, window, "dialogWidth:600px;dialogHeight:400px;");
showModalDialog 為 IE 專有非標準 API,Chrome 37+、Firefox、Edge、Safari 均不支援。現代開發請改用:
// 1. 原生模態對話框 (支援所有現代瀏覽器)
var dialog = document.getElementById('myDialog');
dialog.showModal(); // 模態
dialog.close(); // 關閉
// 2. Bootstrap Modal
$('#myModal').modal('show');
// 3. Layer (國產輕量級)
layer.open({
type: 2,
title: '標題',
area: ['600px', '400px'],
content: 'dialog.html'
});
// 4. SweetAlert2
Swal.fire({
title: '確認',
html: '<iframe src="dialog.html" width="100%" height="300"></iframe>',
showCancelButton: true
});
解決 showModalDialog 快取問題的核心是**禁止瀏覽器快取**。最有效的是伺服器端設定 Cache-Control: no-cache, no-store, must-revalidate 等標頭;輔以頁面端 meta 標籤與 URL 時間戳參數。但強烈建議遷移至現代模態對話框方案,showModalDialog 已不適合生產環境使用。