萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

showModalDialog 快取問題解決方案:禁用瀏覽器快取完整指南

問題根因

showModalDialog(IE 專用、已棄用)開啟的對話方塊預設會快取頁面內容。當資料更新後再次開啟,顯示的仍是舊資料,因為瀏覽器直接讀取快取而非重新請求伺服器。

解決方案:HTTP 快取控制 Meta 標籤

在被開啟的**對話方塊頁面** `` 區塊加入以下 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 標籤作用解析

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 才是根本解決**:

ASP.NET (WebForms / MVC)

// 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";

ASP.NET Core

[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();
});

Java (Servlet / Spring)

// 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() { ... }

PHP

header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");

Node.js (Express)

app.get('/dialog', (req, res) => {
  res.set({
    'Cache-Control': 'no-cache, no-store, must-revalidate',
    'Pragma': 'no-cache',
    'Expires': '0'
  });
  res.render('dialog');
});

替代方案:URL 加入時間戳參數

若無法修改伺服器端,呼叫端每次請求附加隨機參數強制重新載入:

// 開啟對話方塊時
var url = "dialog.aspx?_t=" + new Date().getTime();  // 或 Math.random()
window.showModalDialog(url, window, "dialogWidth:600px;dialogHeight:400px;");

重要提醒:showModalDialog 已棄用

⚠️ 警告: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 已不適合生產環境使用。

copyright © 萬盛學電腦網 all rights reserved