从父窗口刷新子窗口

Bha*_*mar 6 javascript

使用JavaScript

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

当我点击刷新按钮,

我想刷新我的孩子窗口,

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

此代码段重定向页面而不是刷新,

我的东西有一个类似的片段

opener.document.location.reload(真);

但是这一个用于父窗口刷新,但我想要用于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');
}
Run Code Online (Sandbox Code Playgroud)

要打开一个弹出窗口(子窗口),我使用了show_billing_order_form回调,

要刷新子窗口,我在父窗口中添加一个刷新图标,要刷新子窗口,在该刷新图标上点击我名为refresh_my_child_window,

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

T.J*_*der 7

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

var childWindow = window.open(/* ... */);
Run Code Online (Sandbox Code Playgroud)

......当你想要刷新孩子的时候:

childWindow.location.reload();
Run Code Online (Sandbox Code Playgroud)

请注意,childWindow.location.reload如果未从同一源加载父级和子级,某些浏览器将阻止访问.

这是一个快速而肮脏的例子(实时拷贝  - 注意:实时拷贝只能在非编辑模式下工作,比如给定的链接,因为否则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'>
Run Code Online (Sandbox Code Playgroud)

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();
        }
    }
})();
Run Code Online (Sandbox Code Playgroud)

警告:我绝不会建议使用onclick上述属性来连接事件处理程序.相反,我会使用addEventListener(在基于标准的浏览器上)或attachEvent(在IE上),使用像这样的库或实用程序函数.使用上面的属性来避免模糊主要点.