
JS 刷新父页面的方法有多种:使用 window.opener.location.reload()、window.opener.location.href = window.opener.location.href、parent.location.reload()。其中,window.opener.location.reload()是最常用且直接的方式。
window.opener.location.reload() 是一种有效的方法,当你在子窗口(或弹出窗口)中调用这段代码时,它会刷新父窗口的页面。这个方法非常适用于需要在子窗口中进行某些操作后,立即更新父窗口的场景。
一、使用 window.opener.location.reload()
1、基本用法
当你在一个子窗口中进行某些操作后,希望刷新父窗口,可以使用以下代码:
window.opener.location.reload();
window.close();
这段代码会使父窗口重新加载,然后关闭子窗口。
2、使用场景
假设你有一个弹出窗口用于编辑某些数据,当数据编辑完成并保存后,你希望父窗口刷新以显示最新数据。这时 window.opener.location.reload() 就非常适用。
<!-- 父页面 -->
<!DOCTYPE html>
<html>
<head>
<title>父页面</title>
</head>
<body>
<button onclick="openEditWindow()">编辑数据</button>
<script>
function openEditWindow() {
window.open('edit.html', 'editWindow', 'width=600,height=400');
}
</script>
</body>
</html>
<!-- 子页面 edit.html -->
<!DOCTYPE html>
<html>
<head>
<title>编辑数据</title>
</head>
<body>
<form onsubmit="saveData(); return false;">
<input type="text" name="data" required>
<button type="submit">保存</button>
</form>
<script>
function saveData() {
// 假设保存操作成功
alert('数据保存成功');
window.opener.location.reload();
window.close();
}
</script>
</body>
</html>
二、使用 window.opener.location.href = window.opener.location.href
1、基本用法
另一种刷新父页面的方法是重新赋值父窗口的 location.href 属性:
window.opener.location.href = window.opener.location.href;
window.close();
2、使用场景
这种方法和 window.opener.location.reload() 有类似效果,但有时会更合适一些。例如,当你希望刷新父页面但不重新加载脚本或其他资源时,使用 location.href 赋值可能会更高效。
<!-- 父页面 -->
<!DOCTYPE html>
<html>
<head>
<title>父页面</title>
</head>
<body>
<button onclick="openEditWindow()">编辑数据</button>
<script>
function openEditWindow() {
window.open('edit.html', 'editWindow', 'width=600,height=400');
}
</script>
</body>
</html>
<!-- 子页面 edit.html -->
<!DOCTYPE html>
<html>
<head>
<title>编辑数据</title>
</head>
<body>
<form onsubmit="saveData(); return false;">
<input type="text" name="data" required>
<button type="submit">保存</button>
</form>
<script>
function saveData() {
// 假设保存操作成功
alert('数据保存成功');
window.opener.location.href = window.opener.location.href;
window.close();
}
</script>
</body>
</html>
三、使用 parent.location.reload()
1、基本用法
如果子窗口是通过 iframe 嵌入的,那么可以使用 parent.location.reload() 来刷新父窗口:
parent.location.reload();
2、使用场景
当你的子页面是嵌在 iframe 中,而你希望在子页面操作完成后刷新整个父页面,这种方法会很方便。
<!-- 父页面 -->
<!DOCTYPE html>
<html>
<head>
<title>父页面</title>
</head>
<body>
<iframe src="edit.html" name="editFrame" width="600" height="400"></iframe>
</body>
</html>
<!-- 子页面 edit.html -->
<!DOCTYPE html>
<html>
<head>
<title>编辑数据</title>
</head>
<body>
<form onsubmit="saveData(); return false;">
<input type="text" name="data" required>
<button type="submit">保存</button>
</form>
<script>
function saveData() {
// 假设保存操作成功
alert('数据保存成功');
parent.location.reload();
}
</script>
</body>
</html>
四、使用 postMessage 通信刷新父页面
1、基本用法
在一些情况下,子窗口和父窗口可能位于不同的域,这时候可以使用 postMessage 来进行跨域通信,然后在父窗口中接收到消息后进行刷新。
2、使用场景
假设父窗口和子窗口位于不同的域,可以通过 postMessage 进行通信:
<!-- 父页面 -->
<!DOCTYPE html>
<html>
<head>
<title>父页面</title>
</head>
<body>
<button onclick="openEditWindow()">编辑数据</button>
<script>
function openEditWindow() {
window.open('edit.html', 'editWindow', 'width=600,height=400');
}
window.addEventListener('message', function(event) {
if (event.origin === 'https://子窗口的域') {
if (event.data === 'refresh') {
location.reload();
}
}
}, false);
</script>
</body>
</html>
<!-- 子页面 edit.html -->
<!DOCTYPE html>
<html>
<head>
<title>编辑数据</title>
</head>
<body>
<form onsubmit="saveData(); return false;">
<input type="text" name="data" required>
<button type="submit">保存</button>
</form>
<script>
function saveData() {
// 假设保存操作成功
alert('数据保存成功');
window.opener.postMessage('refresh', 'https://父窗口的域');
window.close();
}
</script>
</body>
</html>
通过以上方法,无论是在同域还是跨域的情况下,都可以实现对子页面操作后的父页面刷新。根据具体需求选择合适的方法,能有效提高用户体验和操作效率。
相关问答FAQs:
1. 为什么需要刷新父页面?
刷新父页面通常是在子页面完成某些操作后,需要将更新的数据或状态反映到父页面上。这样可以确保用户在父页面上看到最新的信息。
2. 如何使用JavaScript刷新父页面?
你可以使用以下代码来刷新父页面:
window.opener.location.reload();
这段代码将重新加载父页面,使其显示最新的内容。
3. 如何在子页面中调用刷新父页面的函数?
你可以在子页面中使用以下代码来调用刷新父页面的函数:
function refreshParentPage() {
window.opener.location.reload();
}
然后,在需要刷新父页面的地方,调用refreshParentPage()函数即可。这样可以确保在子页面中完成特定操作后,父页面会被自动刷新。
文章包含AI辅助创作,作者:Edit2,如若转载,请注明出处:https://docs.pingcode.com/baike/3479450