Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.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 子窗口打开后禁用链接,子窗口关闭后恢复_Javascript_Jquery_Html_Parent - Fatal编程技术网

Javascript 子窗口打开后禁用链接,子窗口关闭后恢复

Javascript 子窗口打开后禁用链接,子窗口关闭后恢复,javascript,jquery,html,parent,Javascript,Jquery,Html,Parent,我的客户在他们的网站上有一个链接,在弹出窗口中打开一个客户服务聊天窗口。他们看到用户多次点击聊天链接,打开了多个聊天会话,并且用户的统计信息也随之消失。我需要禁用链接时,聊天窗口打开,并恢复它时,聊天窗口已关闭。我无法修改/访问子窗口 原始链接如下所示: <a class="initChat" onclick="window.open('https://chatlinkhere.com','chatwindow','width=612,height=380,scrollbars=0');

我的客户在他们的网站上有一个链接,在弹出窗口中打开一个客户服务聊天窗口。他们看到用户多次点击聊天链接,打开了多个聊天会话,并且用户的统计信息也随之消失。我需要禁用链接时,聊天窗口打开,并恢复它时,聊天窗口已关闭。我无法修改/访问子窗口

原始链接如下所示:

<a class="initChat" onclick="window.open('https://chatlinkhere.com','chatwindow','width=612,height=380,scrollbars=0'); return false;">
并将链接更改为HTML

<a class="initChat" onclick="openChat();">
但我不知道如何存储它,然后稍后再调用它

接下来,我需要检查聊天窗口是否打开:

timer = setInterval(checkChild, 500);

function checkChild() {
    if (child.open) {
        alert("opened");
        jQuery(".initChat").removeAttr("onclick");
        jQuery(".initChat").css("opacity", ".5");
        clearInterval(timer);
    }

    if (child.closed) {
        alert("closed");
        jQuery(".initChat").attr('onclick', 'openChat(); checkChild();');
        jQuery(".initChat").css("opacity", "1.0");
        clearInterval(timer);
    }
}
注意:警报仅用于测试

并将新函数添加到链接中

<a class="initChat" onclick="openChat(); checkChild();">

当我检查Chrome控制台时,我发现了一个错误
未捕获类型错误:无法读取未定义的属性“open”

更新
不管是谁给我留下了答案,谢谢你,非常有效

我想你需要的是打开弹出窗口,如果已经打开了,那么就专注于弹出窗口,否则不会发生任何事情

您可以将函数重写为

var winPop = false;  
function OpenWindow(url){  
  if(winPop && !winPop.closed){  //checks to see if window is open  
    winPop.focus();  // or nothing 
  }  
  else{  
    winPop = window.open(url,"winPop");  
  }  
}

用一种简单的方法来做。在子窗口打开后禁用锚点链接上的鼠标事件

css

.disableEvents{
    pointer-events: none;
}
var childWindow;

$('a').on('click',function(){
  childWindow = window.open('_blank',"height:200","width:500");
  $(this).addClass('disableEvents');
});

 if (typeof childWindow.attachEvent != "undefined") {
      childWindow.attachEvent("onunload", enableEvents);
 } else if (typeof childWindow.addEventListener != "undefined") {
    childWindow.addEventListener("unload", enableEvents, false);
}

 enableEvents = function(){
      $('a').removeClass('disableEvents');
  };
js

.disableEvents{
    pointer-events: none;
}
var childWindow;

$('a').on('click',function(){
  childWindow = window.open('_blank',"height:200","width:500");
  $(this).addClass('disableEvents');
});

 if (typeof childWindow.attachEvent != "undefined") {
      childWindow.attachEvent("onunload", enableEvents);
 } else if (typeof childWindow.addEventListener != "undefined") {
    childWindow.addEventListener("unload", enableEvents, false);
}

 enableEvents = function(){
      $('a').removeClass('disableEvents');
  };
更新

您的子窗口是纯html页面。在子窗口html代码中进行更改:

<html>
<head>
<script>
function myFunction()
{
window.opener.enableEvents(); //it calls enableEvents function 
}
</script>
</head>
<body onunload="myFunction()">
<!--your content-->
</body>
</html>

函数myFunction()
{
window.opener.enableEvents();//它调用enableEvents函数
}

这就是我最终要做的工作:

    <a class="initChat" onclick="checkWin()"></a>


    <script>
    var myWindow;

    function openWin() {
        myWindow = window.open('https://www.google.com', 'chatwindow', 'width=612,height=380,scrollbars=0');
    }

    function checkWin() {
        if (!myWindow) {
            openWin();
        } else {
            if (myWindow.closed) {
                openWin();
            } else {
                alert('Chat is already opened.');
                myWindow.focus();
            }
        }
    }
    </script>

var-myWindow;
函数openWin(){
myWindow=窗口。打开('https://www.google.com“,”聊天窗口“,”宽度=612,高度=380,滚动条=0”);
}
函数checkWin(){
如果(!myWindow){
openWin();
}否则{
如果(myWindow.closed){
openWin();
}否则{
警报('聊天室已打开');
myWindow.focus();
}
}
}

在Chrome、IE或Firefox中似乎不起作用。我已经用你的答案在我的问题中添加了新的代码片段,也许我实现错了?看起来不错,chandu,但我得到了
未捕获的引用错误:未定义childWindow
。另外,它看起来不像
指针事件
有很好的支持,我确信客户端用户可能使用过时的浏览器=/I忘记定义子窗口。现在我已经添加了我现在得到的
未捕获类型错误:无法读取未定义的属性“attachEvent”
将所有代码放入$(document).ready(){}函数中。如果它不起作用,我们需要更改html代码仍然不起作用,但感谢您的尝试。我将所有html(包括脚本)粘贴到