Javascript 从父窗口刷新子窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4059179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:18:55  来源:igfitidea点击:

refresh child window from parent window

javascript

提问by Bharanikumar

Using JavaScript

使用 JavaScript

i have the refresh button in my parent window,

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

when i click refresh button ,

当我点击刷新按钮时,

i want to refresh my child window ,

我想刷新我的子窗口,

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

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

This snippet redirecting the page instead refresh ,

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

I thing there is a snippet like

我的事情有一个片段像

opener.document.location.reload(true);

opener.document.location.reload(true);

but this one for parent window refresh, but i want for child window wiht URL location option

但是这个用于父窗口刷新,但我想要带有 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');
}

To open a popup window(child window) , i used this show_billing_order_form call back ,

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

To refresh the child window , i add one refresh icon in my parent window , To refresh the child window , in that refresh icon onclick i called refresh_my_child_window ,

要刷新子窗口,我在父窗口中添加一个刷新图标,要刷新子窗口,在该刷新图标 onclick 中,我调用了 refresh_my_child_window ,

but function refreshing my child window..

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

采纳答案by T.J. Crowder

When opening your child window from the parent, remember the return value in a variable somewhere:

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

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

...and when you want to refresh the child:

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

childWindow.location.reload();

Note that some browsers will prevent access to childWindow.location.reloadif the parent and child aren't loaded from the same origin.

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

Here's a quick-and-dirty example (live copy — note: the live copy only works in non-edit mode, like the link given, because otherwise JSBin uses null.jsbin.cominstead of output.jsbin.comand so the origin doesn't match):

这是一个快速而肮脏的例子(实时复制 ——注意:实时复制只在非编辑模式下工作,就像给定的链接,因为否则 JSBin 使用null.jsbin.com而不是,output.jsbin.com所以来源不匹配):

HTML:

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:

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();
        }
    }
})();

Caveat: I would never recommend hooking up event handlers with onclickproperties as above. Instead, I'd use addEventListener(on standards-based browsers) or attachEvent(on IE), by using a library or a utility function like this one. Used the properties above to avoid obscuring the main point.

警告:我永远不会建议将事件处理程序与onclick上述属性挂钩。相反,我会使用addEventListener(在基于标准的浏览器上)或attachEvent(在 IE 上),通过使用像这样的库或实用程序函数。使用上述属性以避免混淆要点。

回答by Deepika Patel

var childWindow = window.open("","name",/* .. */);
childWindow.location.reload();