0
I’m in trouble, I want to open a window with window.open
in a chat, this is how, when a user sends a message, the main panel receives the message by socket
using websocket.onmessage
, then opens a modal for the other person to see that someone sent this message, and in the modal has an event onclick
which 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 ID
correspondent is already open, not to be showing the direct message even being already with the chat window open.
So 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');
});
}
}
There in the socket
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 show nothing.
The problem is that if the user on the other side, send for example 5 messages, IE, the function will be called 5 times, the modal will be shown normally, but when clicking on it, will open 5 windows equal, but I just want to open only one window, even if the user sends 10 messages, making the function run 10 times, when clicking on the modal open only 1 Popup and not 10 as it is occurring, it is as if it were cached.
What if you made an IF before opening the popup? Type compare if the received ID is already open...
– Sam
But you already have that in the job
OpenWindow
, and still opens a lot of windows.– Cassiano José
And if you declare
winPop = true;
when the window opens for the first time since you declared itfalse
before?– Sam
Yes, I have declared her as
false
and within theelse
I put it astrue
, then the modal only opened once, even if the other user sent more messages, the modal did not update the messages, but when clicking to open the popup, it opened a numberX
windows, which was equivalent to the number of messages sent. ie, did not work also!– Cassiano José
It’s like I’m looping the function according to the number of messages.
– Sam
that’s it! and I can’t think of a solution!
– Cassiano José
See if it works except the
"_blank",
popup– Sam
That I know the syntax to open popup with window.open is (url,filename,attributes)... I think "_Blank" is left there..
– Sam
I also thought it was... but it’s not! when I shoot the "_Blank", the window opens in a new tab, not in popup.
– Cassiano José
Instead of +idc+', just put the idc and put a single quote after the comma, like this:
winPop = window.open(url,"_blank",idc,'scrollbars=1,menubar=0,resizable=1,width=850,height=500');
});– Sam