I have a problem, I want to open a window with window.open
in a chat, so when a user sends a message, the main panel receives the message by socket
using websocket.onmessage
, modal to the other person to see that someone sent this message, and in the modal has a onclick
event that opens a window using window.open
, so far so good, the problem is that I want to open this window
which is assigned a ID
only once, that is, if the other person sends a new message I check if the corresponding ID
window is already open, not to be showing the direct message even though the chat window is open. / p>
Then I created the following function:
var winPop = false;
function OpenWindow(msgdoc,url,idc){
if(winPop && !winPop.closed){ //checka se a janela já está aberta
winPop.focus();
} else{
$("#chattext").text(msgdoc);
$("#cli").trigger('click');
//Abre a janela quando o usuário clicar no modal
$("#chat").click(function(){
$(this).hide("slow");
winPop = window.open(url,"_blank",+idc+',scrollbars=1,menubar=0,resizable=1,width=850,height=500');
});
}
}
Then in% w_that I did the following:
websocket.onmessage = function(ev) {
var msg = JSON.parse(ev.data); //Recebe os dados enviados pelo socket PHP
var type = msg.type; //Recebe o Tipo de mensagem
var umsg = msg.message; //Recebe Mensagem
var de_id = msg.de_id; //Recebe de_id
var url = 'http://siteta.com/chat/index.php?id='+de_id+'&nome='+uname+'&port=8080';
OpenWindow(umsg,url,de_id);
}
That is, whenever you receive a new message, it will check if the window is already open, if it is not, it will show the modal, otherwise it will not display anything.
The problem is that if the user on the other side send 5 messages for example, ie the function will be called 5 times, the modal will be shown normally, but clicking on it will open 5 windows, but I just want it to open only one window, even if the user sends 10 messages, doing the function if it executes 10 times, when clicking the modal open only 1 PopUp and not 10 as it is occurring, it is as if I were cached I do not know.