使用JavaScript
我的父窗口中有刷新按钮,
当我点击刷新按钮时,
我想刷新我的子窗口,
window.location.href='add_billing_order_frm.php?session_re_genrate=new
此代码段重定向页面而不是刷新,
我觉得有一段像
opener.document.location.reload(true);
但是这个用于父窗口刷新,但我想要带有 URL 位置选项的子窗口
function show_billing_order_form(url){
var childWindow = window.open(url);
}
function refresh_my_child_window(){
return childWindow.location.reload('add_billing_order_frm.php');
}
为了打开一个弹出窗口(子窗口),我使用了这个 show_billing_order_form 回调,
要刷新子窗口,我在父窗口中添加一个刷新图标,要刷新子窗口,在那个刷新图标 onclick 中我调用了 refresh_my_child_window,
但是函数刷新了我的子窗口..
最佳答案
从父窗口打开子窗口时,记住某处变量中的返回值:
var childWindow = window.open(/* ... */);
...当你想刷新 child 时:
childWindow.location.reload();
请注意,如果未从 same origin 加载父项和子项,某些浏览器将阻止访问 childWindow.location.reload
.
这是一个简单的示例(live copy — 注意:Live Copy 仅在非编辑模式下工作,就像给定的链接一样,否则 JSBin 使用 null.jsbin.com
而不是 output.jsbin.com
,所以源不匹配):
HTML:
<input type='button' id='btnOpen' value='Open Child'>
<input type='button' id='btnClose' value='Close Child'>
<input type='button' id='btnRefresh' value='Refresh Child'>
JavaScript:
(function() {
var childWindow;
document.getElementById('btnOpen').onclick = openChildWindow;
document.getElementById('btnClose').onclick = closeChildWindow;
document.getElementById('btnRefresh').onclick = refreshChildWindow;
function openChildWindow() {
if (childWindow) {
alert("We already have one open.");
} else {
childWindow = window.open(location.protocol + "//" + location.host + "/cotokijigu/1");
}
}
function closeChildWindow() {
if (!childWindow) {
alert("There is no child window open.");
}
else {
childWindow.close();
childWindow = undefined;
}
}
function refreshChildWindow() {
if (!childWindow) {
alert("There is no child window open.");
} else {
childWindow.location.reload();
}
}
})();
警告:我绝不会推荐将事件处理程序与上面的 onclick
属性 Hook 。相反,我会使用 addEventListener
(在基于标准的浏览器上)或 attachEvent
(在 IE 上),通过使用一个库或实用函数,如 this one .使用上述属性以避免混淆要点。
关于javascript - 从父窗口刷新子窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4059179/