Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.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从父onblur关闭子窗口_Javascript_Window_Onblur - Fatal编程技术网

使用Javascript从父onblur关闭子窗口

使用Javascript从父onblur关闭子窗口,javascript,window,onblur,Javascript,Window,Onblur,我有一个在弹出窗口(子窗口)中返回其他页面(谷歌)的代码。我不能在子窗口中编写任何代码,因此我必须从父窗口执行所有操作。我正在尝试将onblur事件从父窗口绑定到子窗口,以便子窗口在失去焦点时应关闭。但是,子窗口会飞溅一微秒并自动关闭 我不能在我工作的环境中使用jQuery 以下是我的示例代码: <html> <head> </head> <body> <input type="button" value="Open Google" onc

我有一个在弹出窗口(子窗口)中返回其他页面(谷歌)的代码。我不能在子窗口中编写任何代码,因此我必须从父窗口执行所有操作。我正在尝试将onblur事件从父窗口绑定到子窗口,以便子窗口在失去焦点时应关闭。但是,子窗口会飞溅一微秒并自动关闭

我不能在我工作的环境中使用jQuery

以下是我的示例代码:

<html>
<head>
</head>

<body>
<input type="button" value="Open Google" onclick="OpenWindow()" />

<script type="text/javascript">

function OpenWindow()
{
    var url="http://www.google.com";

    var newWin=window.open(url, 'Popup','height=500,width=600,left=100,top=100,resizable=yes,scrollbars=no,toolbar=no,status=no');
    newWin.focus();
    newWin.onblur =  newWin.close();    
}
</script>

</body>
</html>

函数OpenWindow()
{
变量url=”http://www.google.com";
var newWin=window.open(url,'Popup','height=500,width=600,left=100,top=100,resizable=yes,scrollbars=no,toolbar=no,status=no');
newWin.focus();
newWin.onblur=newWin.close();
}

跳过
.close()后面的括号即可。

如果要传递函数引用,则不希望分配通过执行
.close()

所返回的值


使用“window.top.close();”

另一个答案如下:

function open_popup() {
                var my_window = window.open("", 'targetWindow', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=750px,height=500px');
                var html = '';
                html = "Muhammad Shoaib Qureshi's New Window Text";
                my_window.document.write(html);
                my_window.focus();
                my_window.onblur = function() {
                    my_window.close();
                };
            }
除了上面这一个,没有其他解决方案对我有效

问候,, Shoaib.

上述解决方案将不再适用于跨源域,并可能引发此错误
阻止了具有原点的帧”https://example.com“从访问交叉原点帧开始。

为了解决这个问题,我们可以向父窗口添加一个事件侦听器,并侦听
焦点
事件,这应该在用户从衍生子窗口单击返回主窗口后触发,然后我们可以关闭框架

 let url = "https://example.com";
 let frame = window.open(url, "Popup", "height=500,width=600,left=100,top=100,resizable=yes,scrollbars=no,toolbar=no,status=no");
 window.addEventListener('focus', (e) => {
     if (!frame.closed) frame.close()
 })
 let url = "https://example.com";
 let frame = window.open(url, "Popup", "height=500,width=600,left=100,top=100,resizable=yes,scrollbars=no,toolbar=no,status=no");
 window.addEventListener('focus', (e) => {
     if (!frame.closed) frame.close()
 })