【问题标题】:refresh child window from parent window从父窗口刷新子窗口
【发布时间】:2010-10-30 14:45:18
【问题描述】:

使用 JavaScript

我的父窗口中有刷新按钮,

当我点击刷新按钮时,

我想刷新我的子窗口,

window.location.href='add_billing_order_frm.php?session_re_genrate=new

这个 sn-p 重定向页面而不是刷新,

我觉得有一个像sn-p这样的

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 回调,

要刷新子窗口,我在父窗口中添加了一个刷新图标,要刷新子窗口,在该刷新图标中单击我称为 refresh_my_child_window,

但功能刷新我的子窗口..

【问题讨论】:

    标签: javascript


    【解决方案1】:

    从父窗口打开子窗口时,请记住某处变量中的返回值:

    var childWindow = window.open(/* ... */);
    

    ...当你想刷新孩子时:

    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 属性连接事件处理程序。相反,我会使用 addEventListener(在基于标准的浏览器上)或 attachEvent(在 IE 上),通过使用像 this one 这样的库或实用程序函数。使用上面的属性以避免模糊重点。

    【讨论】:

    • @PiyushBalapure - 确实如此,只是 JSBin 从 http 切换到 https ,因此示例停止工作,因为父级在 https 上,但子级以 http 打开.我已经更新了实时示例链接。
    【解决方案2】:
    var childWindow = window.open("","name",/* .. */);
    childWindow.location.reload();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多