我的客户在他们的网站上有一个链接,在弹出窗口中打开一个客户服务聊天窗口。他们看到用户多次点击聊天链接,这会打开多个聊天会话,并且会丢弃他们的数据。我需要在打开聊天窗口时禁用链接,并在聊天窗口关闭时恢复链接。我无法修改/访问子窗口。
原始链接如下所示:
<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/
当我检查Console时,我得到了错误的Uncaught TypeError: Cannot read property 'open' of undefined
UPDATE --谁在http://jsfiddle.net/JkthJ/2/给我留下了答案--非常感谢
发布于 2014-06-13 16:20:27
这就是我最终要做的:
<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>发布于 2014-06-13 14:00:10
我想你需要的是打开,弹出,如果已经打开,就会弹出来或者什么都不会发生。
您可以将您的函数重写为
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");
}
}发布于 2014-06-13 14:15:52
用一种简单的方式去做。打开子窗口后,禁用锚链接上的鼠标事件。
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>https://stackoverflow.com/questions/24206859
复制相似问题