Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript中使用window.open将值从子窗口返回到父窗口?_Javascript_Jquery_Html_Dom - Fatal编程技术网

如何在Javascript中使用window.open将值从子窗口返回到父窗口?

如何在Javascript中使用window.open将值从子窗口返回到父窗口?,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,我有一个父窗口,我在其中使用window.open打开子窗口,如下所示。我想从子窗口获取一个布尔值,在此基础上我将在父窗口中执行任务 function openChildWin() { var childWin = window.open("childWin.html", "_blank", "height=400, width=550, status=yes, toolbar=no, menubar=no, location=no,addressbar=no); if

我有一个父窗口,我在其中使用window.open打开子窗口,如下所示。我想从子窗口获取一个布尔值,在此基础上我将在父窗口中执行任务

function openChildWin() 
{   
    var childWin = window.open("childWin.html", "_blank", "height=400, width=550, status=yes, toolbar=no, menubar=no, location=no,addressbar=no); 
    if (childWin)
    {
        //some logic
    }
    else
    {
        //some logic
    }
} 

**childWin.html**



 <!DOCTYPE html>
    <html lang="en">
      <head>

        <script type="text/javascript">

            function OKClicked()
            {
                //return true; //I want to return true here
                            window.close(); //Close this child window  
            }

        </script>

      </head>
      <body>
        <button type="button" id="OKButton" onclick="OKClicked()">Click ME</button>
      </body>
     </html>
函数openChildWin() { var childWin=window.open(“childWin.html”,“_blank”,“高度=400,宽度=550,状态=yes,工具栏=no,菜单栏=no,位置=no,地址栏=no); 如果(childWin) { //一些逻辑 } 其他的 { //一些逻辑 } } **childWin.html** 函数OKClicked() { //return true;//我想在这里返回true window.close();//关闭此子窗口 } 点击我
单击按钮时,我想从这里返回true。但所有这些对我来说都不起作用。我在这里缺少语法吗?

您可以这样做:

在父项中:

function openChildWin() {   
    var childWin = window.open("childWin.html", "_blank", "height=400, width=550, status=yes, toolbar=no, menubar=no, location=no,addressbar=no"); 
}
function setValue(val1) {
   // do your logic here
}
在弹出窗口中:

function OKClicked() {
    window.opener.setValue(true);
}

使用下面的代码并用元素Id替换测试

var parentDoc = window.opener.document;
parentDoc.getElementById("test").value = document.getElementById("test").value;

你不能尝试使用Cookie吗???@Amarnathrsenoy-在这里使用Cookie有什么乐趣。我只需要从child捕获一个bool变量,这样我就可以在父窗口上做进一步的操作。看看@Ashan shah-这不起作用。window.open语句下面的语句会在child窗口打开后立即执行。check已修改。您可以使用设置值来实现logic@Ashan沙阿-是的,就是现在。我已经实现了。谢谢你的回答。