location.reload()
// 強制從伺服器重新載入 (相當於 F5)
window.location.reload(true);
// 僅從快取載入 (預設)
window.location.reload(false);
// 或
window.location.reload();
// 方式 1:contentWindow.location.reload() (推薦)
document.getElementById('myframe').contentWindow.location.reload(true);
// 方式 2:重新設定 src (會觸發 onload)
document.getElementById('myframe').src = document.getElementById('myframe').src;
// 方式 3:contentDocument.location.reload() (較少用)
document.getElementById('myframe').contentDocument.location.reload(true);
// 方式 1:attr 重設 src (觸發重載)
$('#myframe').attr('src', $('#myframe').attr('src'));
// 方式 2:呼叫原生 reload
$('#myframe')[0].contentWindow.location.reload(true);
// 封裝函式
function refreshIframe(iframeSelector) {
var $iframe = $(iframeSelector);
if ($iframe.length) {
$iframe[0].contentWindow.location.reload(true);
}
}
// 使用
refreshIframe('#myframe');
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery 刷新 iframe 示範</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<iframe id="myframe" width="100%" height="400" frameborder="0" src="test.html"></iframe>
<br><br>
<button onclick="refreshFrame1()">方法1: contentWindow.reload()</button>
<button onclick="refreshFrame2()">方法2: jQuery attr src 重設</button>
<button onclick="refreshFrame3()">方法3: 原生 src 重設</button>
<script>
function refreshFrame1() {
document.getElementById('myframe').contentWindow.location.reload(true);
}
function refreshFrame2() {
$('#myframe').attr('src', $('#myframe').attr('src'));
}
function refreshFrame3() {
var iframe = document.getElementById('myframe');
iframe.src = iframe.src;
}
</script>
</body>
</html>
| 場景 | 代碼 | 說明 |
|---|---|---|
| iframe 刷新父視窗 | parent.location.reload(); | 子頁面呼叫 |
| 子視窗刷新開啟它的視窗 | self.opener.location.reload(); | window.open 開啟的彈窗 |
| 刷新同層級另一個 iframe | parent.otherFrameName.location.reload(); | 需知道目標 iframe name |
| 刷新頂層視窗 | top.location.reload(); | 穿透多層 iframe |
document.frames['name'] 不可用舊代碼常見 document.frames('ifrmname').location.reload(),**現代瀏覽器已不支援**。請改用 window.frames['name'] 或 document.getElementById('id').contentWindow。
不同源的 iframe 無法存取 contentWindow。解決方案:
// 父視窗:重新設定 src (跨域也可操作)
$('#crossDomainIframe').attr('src', function(i, val) {
return val + (val.indexOf('?') > -1 ? '&' : '?') + '_t=' + Date.now();
});
// 子視窗:postMessage 通知父視窗重載
window.parent.postMessage({action: 'reloadIframe', id: 'myframe'}, '*');
// 保存捲動位置
var scrollTop = $('#myframe')[0].contentWindow.document.documentElement.scrollTop;
// 重載後恢復 (需在 iframe 內部頁面執行)
$(window).on('load', function() {
if (sessionStorage.scrollTop) {
document.documentElement.scrollTop = sessionStorage.scrollTop;
sessionStorage.removeItem('scrollTop');
}
});
$('#container').load('/api/content')iframe.srcdoc:直接設定 HTML 內容(同源)| 需求 | 推薦方法 |
|---|---|
| 同源 iframe 完整重載 | iframe.contentWindow.location.reload(true) |
| 跨域 iframe 強制重載 | 重設 src 加時間戳參數 |
| 無感局部更新 | AJAX / Fetch + DOM 更新 |
| 保持捲動位置 | sessionStorage 暫存 + onload 恢復 |
核心原則:能用 AJAX 局部更新就不要重載 iframe;必須重載時,同源用 reload(true),跨域用 src 重設。