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



我的客户在他们的网站上有一个链接,在弹出窗口中打开一个客户服务聊天窗口。他们看到用户多次点击聊天链接,这打开了多个聊天会话,这是他们的统计数据。我需要禁用链接时,聊天窗口打开,并恢复它时,聊天窗口已关闭。我不能修改/访问子窗口。

原链接如下:
<a class="initChat" onclick="window.open('https://chatlinkhere.com','chatwindow','width=612,height=380,scrollbars=0'); return false;">

我认为最好的办法是将window.open()存储为函数中的变量:

function openChat() {
    child = window.open('http://www.google.com', 'chatwindow', 'width=612,height=380,scrollbars=0,menubar=0');
}
并将链接HTML更改为
<a class="initChat" onclick="openChat();">

注意:理想情况下,我想检测原始的onclick的值,并将其存储在一个变量中。比如:

  jQuery('.initChat').find().attr('onclick');

但是我不确定如何存储它,然后稍后调用它。

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

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

一旦聊天窗口关闭,我需要恢复链接的onclick属性(是否有更简单的方法来做到这一点?)

小提琴演示在这里-> http://jsfiddle.net/JkthJ/

当我检查Chrome控制台时,我得到错误Uncaught TypeError: Cannot read property 'open' of undefined

更新谁给我的答案在http://jsfiddle.net/JkthJ/2/非常感谢你,它的工作!:)

我认为你需要的是打开弹出窗口如果已经打开了,那么关注弹出窗口,否则什么都不会发生

你可以将函数重写为

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

js

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>

这是我最后要做的:

    <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>

最新更新