Check array for duplicate values if cookies aren't set, don't check if cookies are set












1















I'm working on a chatroom page where you first set your username and it sets the username value inside cookies. after this it loads different input boxes where you can send messages from in realtime. At this point you can reload the page and it will give you a pop-up which will display a welcome back message with the name value saved inside the cookies, which will also just set it as the username again and load the chatting page.



The problem is that I want to be able to prevent multiple duplicates of a name to be set from the username setting input field, but allow the username be set multiple times inside the array via cookies.



this function sets the original username:



//index.html
function setUsername() {
var textbox = document.getElementById("name");
var txtboxval = textbox.value;
if(textbox.value.length <= 20 && textbox.value.length >= 4){
socket.emit('setUsername', document.getElementById('name').value);
setCookie("username", txtboxval, 30);
socket.on('userExists', function(data) {

});
}
else{
alert("The name has to be at least 4 characters long");
return false;
}
};


and these two set it on cookie load:



  //index.html
function setUsername2() {
var nimi = getCookie('username');
socket.emit('setUsername', nimi);
}

function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
setUsername2();
document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Write your message here">
<button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
<div id = "message-container"></div></div>';
}
}


now this here actually sets the username into the array where it digs it from on the index.html page:



  //app.js
users = ;
io.on('connection', function(socket){
console.log('an user connected');
console.log(users);
socket.on('setUsername', function(data) {
console.log(data);

users.push(data);
socket.emit('userSet', {username: data});
console.log(users);
});


I would like this piece of code to run when first setting the username, but not when it loads from cookies:



//app.js
if(users.indexOf(data) > -1) {
socket.emit('userExists', data + ' Username is already taken.');
} else {
users.push(data);
socket.emit('userSet', {username: data});
console.log(users);
}


Is there something I'm missing on why I can't get it to work as I would like it to? If something is unclear please ask.










share|improve this question





























    1















    I'm working on a chatroom page where you first set your username and it sets the username value inside cookies. after this it loads different input boxes where you can send messages from in realtime. At this point you can reload the page and it will give you a pop-up which will display a welcome back message with the name value saved inside the cookies, which will also just set it as the username again and load the chatting page.



    The problem is that I want to be able to prevent multiple duplicates of a name to be set from the username setting input field, but allow the username be set multiple times inside the array via cookies.



    this function sets the original username:



    //index.html
    function setUsername() {
    var textbox = document.getElementById("name");
    var txtboxval = textbox.value;
    if(textbox.value.length <= 20 && textbox.value.length >= 4){
    socket.emit('setUsername', document.getElementById('name').value);
    setCookie("username", txtboxval, 30);
    socket.on('userExists', function(data) {

    });
    }
    else{
    alert("The name has to be at least 4 characters long");
    return false;
    }
    };


    and these two set it on cookie load:



      //index.html
    function setUsername2() {
    var nimi = getCookie('username');
    socket.emit('setUsername', nimi);
    }

    function checkCookie() {
    var user = getCookie("username");
    if (user != "") {
    alert("Welcome again " + user);
    setUsername2();
    document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Write your message here">
    <button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
    <div id = "message-container"></div></div>';
    }
    }


    now this here actually sets the username into the array where it digs it from on the index.html page:



      //app.js
    users = ;
    io.on('connection', function(socket){
    console.log('an user connected');
    console.log(users);
    socket.on('setUsername', function(data) {
    console.log(data);

    users.push(data);
    socket.emit('userSet', {username: data});
    console.log(users);
    });


    I would like this piece of code to run when first setting the username, but not when it loads from cookies:



    //app.js
    if(users.indexOf(data) > -1) {
    socket.emit('userExists', data + ' Username is already taken.');
    } else {
    users.push(data);
    socket.emit('userSet', {username: data});
    console.log(users);
    }


    Is there something I'm missing on why I can't get it to work as I would like it to? If something is unclear please ask.










    share|improve this question



























      1












      1








      1








      I'm working on a chatroom page where you first set your username and it sets the username value inside cookies. after this it loads different input boxes where you can send messages from in realtime. At this point you can reload the page and it will give you a pop-up which will display a welcome back message with the name value saved inside the cookies, which will also just set it as the username again and load the chatting page.



      The problem is that I want to be able to prevent multiple duplicates of a name to be set from the username setting input field, but allow the username be set multiple times inside the array via cookies.



      this function sets the original username:



      //index.html
      function setUsername() {
      var textbox = document.getElementById("name");
      var txtboxval = textbox.value;
      if(textbox.value.length <= 20 && textbox.value.length >= 4){
      socket.emit('setUsername', document.getElementById('name').value);
      setCookie("username", txtboxval, 30);
      socket.on('userExists', function(data) {

      });
      }
      else{
      alert("The name has to be at least 4 characters long");
      return false;
      }
      };


      and these two set it on cookie load:



        //index.html
      function setUsername2() {
      var nimi = getCookie('username');
      socket.emit('setUsername', nimi);
      }

      function checkCookie() {
      var user = getCookie("username");
      if (user != "") {
      alert("Welcome again " + user);
      setUsername2();
      document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Write your message here">
      <button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
      <div id = "message-container"></div></div>';
      }
      }


      now this here actually sets the username into the array where it digs it from on the index.html page:



        //app.js
      users = ;
      io.on('connection', function(socket){
      console.log('an user connected');
      console.log(users);
      socket.on('setUsername', function(data) {
      console.log(data);

      users.push(data);
      socket.emit('userSet', {username: data});
      console.log(users);
      });


      I would like this piece of code to run when first setting the username, but not when it loads from cookies:



      //app.js
      if(users.indexOf(data) > -1) {
      socket.emit('userExists', data + ' Username is already taken.');
      } else {
      users.push(data);
      socket.emit('userSet', {username: data});
      console.log(users);
      }


      Is there something I'm missing on why I can't get it to work as I would like it to? If something is unclear please ask.










      share|improve this question
















      I'm working on a chatroom page where you first set your username and it sets the username value inside cookies. after this it loads different input boxes where you can send messages from in realtime. At this point you can reload the page and it will give you a pop-up which will display a welcome back message with the name value saved inside the cookies, which will also just set it as the username again and load the chatting page.



      The problem is that I want to be able to prevent multiple duplicates of a name to be set from the username setting input field, but allow the username be set multiple times inside the array via cookies.



      this function sets the original username:



      //index.html
      function setUsername() {
      var textbox = document.getElementById("name");
      var txtboxval = textbox.value;
      if(textbox.value.length <= 20 && textbox.value.length >= 4){
      socket.emit('setUsername', document.getElementById('name').value);
      setCookie("username", txtboxval, 30);
      socket.on('userExists', function(data) {

      });
      }
      else{
      alert("The name has to be at least 4 characters long");
      return false;
      }
      };


      and these two set it on cookie load:



        //index.html
      function setUsername2() {
      var nimi = getCookie('username');
      socket.emit('setUsername', nimi);
      }

      function checkCookie() {
      var user = getCookie("username");
      if (user != "") {
      alert("Welcome again " + user);
      setUsername2();
      document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Write your message here">
      <button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
      <div id = "message-container"></div></div>';
      }
      }


      now this here actually sets the username into the array where it digs it from on the index.html page:



        //app.js
      users = ;
      io.on('connection', function(socket){
      console.log('an user connected');
      console.log(users);
      socket.on('setUsername', function(data) {
      console.log(data);

      users.push(data);
      socket.emit('userSet', {username: data});
      console.log(users);
      });


      I would like this piece of code to run when first setting the username, but not when it loads from cookies:



      //app.js
      if(users.indexOf(data) > -1) {
      socket.emit('userExists', data + ' Username is already taken.');
      } else {
      users.push(data);
      socket.emit('userSet', {username: data});
      console.log(users);
      }


      Is there something I'm missing on why I can't get it to work as I would like it to? If something is unclear please ask.







      javascript node.js cookies socket.io






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 30 '18 at 9:44







      Veraen

















      asked Nov 28 '18 at 9:31









      VeraenVeraen

      3011




      3011
























          2 Answers
          2






          active

          oldest

          votes


















          1





          +100









          You could get this done triggering your 'setUsername' using an object {name: string, cookie: boolean} instead of the username only, like i did here:



          function setUsername() {
          var textbox = document.getElementById("name");
          var txtboxval = textbox.value;
          if(textbox.value.length <= 20 && textbox.value.length >= 4){
          socket.emit('setUsername', {name: document.getElementById('name').value, cookie: false});
          setCookie("username", txtboxval, 30);
          socket.on('userExists', function(data) {
          // ########## you could add your 'already taken'-message here. :) ##########
          console.log(data);
          document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
          // this 'deletes' the cookie.
          });
          } else {
          alert("The name has to be at least 4 characters long");
          return false;
          }
          }




          function setUsername2() {
          var nimi = getCookie('username');
          socket.emit('setUsername', {name: nimi, cookie: true});
          }

          function checkCookie() {
          var user = getCookie("username");
          if (user != "") {
          alert("Welcome again " + user);
          setUsername2();
          document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Write your message here">
          <button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
          <div id = "message-container"></div></div>';
          }
          }




          users = ;
          io.on('connection', function(socket){
          console.log('an user connected');
          console.log(users);
          socket.on('setUsername', function(data) {
          console.log(data);

          if(!data.cookie && users.indexOf(data.name) > -1) {
          socket.emit('userExists', data.name + ' Username is already taken.');
          } else {
          users.push(data.name);
          socket.emit('userSet', {username: data.name});
          console.log(users);
          }
          });
          });





          share|improve this answer


























          • This gives me reference errors when trying to set the name, "Uncaught ReferenceError: setUsername is not defined at HTMLButtonElement.onclick " and "(index):291 Uncaught ReferenceError: checkCookie is not defined at onload " also it says that somewhere in the setUsername function there's a missing ) I can't find it though.

            – Veraen
            Nov 30 '18 at 10:05











          • SyntaxErrors are fixed now. My fault. :)

            – J. Sadi
            Nov 30 '18 at 10:09











          • ah :), I tried after syntax fixes and this actually works, 1 more thing, where in there could I give a message that would say that the name is already in use?

            – Veraen
            Nov 30 '18 at 10:10






          • 1





            Tried it just now, this works, I don't see any more issues being there so thank you very much I've been stuck on this problem for days! Thanks a huge bunch! I will award the bounty to you once it becomes available :)

            – Veraen
            Nov 30 '18 at 10:54








          • 1





            you're welcome :)

            – J. Sadi
            Nov 30 '18 at 10:55



















          0














          If i got it right, and you want to distinct two activities - setting username from cookie and setting username from input#name -, so you just have to emit userExists with some additional data, with some flag, like cookie (which will mean that you're setting the value from cookie) or user_input (which will mean that you're setting the value from the input field), like this:



          socket.emit('userSet', {username: data.name, user_input: true});





          share|improve this answer
























          • Mind giving an example on the part on how I should do it? I don't exactly understand how I would build the syntax.

            – Veraen
            Nov 30 '18 at 10:07











          • @Veraen, first part: you have 2 ways of setting cookies - from input and from cookies. Second part: you catch userSet event and extract value from it. But you can't say where did the event come from. So you have to put additional data in it, just to be able to say where did it come from: from input or from cookies. Now, when you can distinct username sources, you decide if you want to emit userExists or no. That's it!

            – lucifer63
            Nov 30 '18 at 10:12











          • Thank you for the answer! But I've got my solution from J. Sadi's answer.

            – Veraen
            Nov 30 '18 at 10:55











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516191%2fcheck-array-for-duplicate-values-if-cookies-arent-set-dont-check-if-cookies-a%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1





          +100









          You could get this done triggering your 'setUsername' using an object {name: string, cookie: boolean} instead of the username only, like i did here:



          function setUsername() {
          var textbox = document.getElementById("name");
          var txtboxval = textbox.value;
          if(textbox.value.length <= 20 && textbox.value.length >= 4){
          socket.emit('setUsername', {name: document.getElementById('name').value, cookie: false});
          setCookie("username", txtboxval, 30);
          socket.on('userExists', function(data) {
          // ########## you could add your 'already taken'-message here. :) ##########
          console.log(data);
          document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
          // this 'deletes' the cookie.
          });
          } else {
          alert("The name has to be at least 4 characters long");
          return false;
          }
          }




          function setUsername2() {
          var nimi = getCookie('username');
          socket.emit('setUsername', {name: nimi, cookie: true});
          }

          function checkCookie() {
          var user = getCookie("username");
          if (user != "") {
          alert("Welcome again " + user);
          setUsername2();
          document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Write your message here">
          <button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
          <div id = "message-container"></div></div>';
          }
          }




          users = ;
          io.on('connection', function(socket){
          console.log('an user connected');
          console.log(users);
          socket.on('setUsername', function(data) {
          console.log(data);

          if(!data.cookie && users.indexOf(data.name) > -1) {
          socket.emit('userExists', data.name + ' Username is already taken.');
          } else {
          users.push(data.name);
          socket.emit('userSet', {username: data.name});
          console.log(users);
          }
          });
          });





          share|improve this answer


























          • This gives me reference errors when trying to set the name, "Uncaught ReferenceError: setUsername is not defined at HTMLButtonElement.onclick " and "(index):291 Uncaught ReferenceError: checkCookie is not defined at onload " also it says that somewhere in the setUsername function there's a missing ) I can't find it though.

            – Veraen
            Nov 30 '18 at 10:05











          • SyntaxErrors are fixed now. My fault. :)

            – J. Sadi
            Nov 30 '18 at 10:09











          • ah :), I tried after syntax fixes and this actually works, 1 more thing, where in there could I give a message that would say that the name is already in use?

            – Veraen
            Nov 30 '18 at 10:10






          • 1





            Tried it just now, this works, I don't see any more issues being there so thank you very much I've been stuck on this problem for days! Thanks a huge bunch! I will award the bounty to you once it becomes available :)

            – Veraen
            Nov 30 '18 at 10:54








          • 1





            you're welcome :)

            – J. Sadi
            Nov 30 '18 at 10:55
















          1





          +100









          You could get this done triggering your 'setUsername' using an object {name: string, cookie: boolean} instead of the username only, like i did here:



          function setUsername() {
          var textbox = document.getElementById("name");
          var txtboxval = textbox.value;
          if(textbox.value.length <= 20 && textbox.value.length >= 4){
          socket.emit('setUsername', {name: document.getElementById('name').value, cookie: false});
          setCookie("username", txtboxval, 30);
          socket.on('userExists', function(data) {
          // ########## you could add your 'already taken'-message here. :) ##########
          console.log(data);
          document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
          // this 'deletes' the cookie.
          });
          } else {
          alert("The name has to be at least 4 characters long");
          return false;
          }
          }




          function setUsername2() {
          var nimi = getCookie('username');
          socket.emit('setUsername', {name: nimi, cookie: true});
          }

          function checkCookie() {
          var user = getCookie("username");
          if (user != "") {
          alert("Welcome again " + user);
          setUsername2();
          document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Write your message here">
          <button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
          <div id = "message-container"></div></div>';
          }
          }




          users = ;
          io.on('connection', function(socket){
          console.log('an user connected');
          console.log(users);
          socket.on('setUsername', function(data) {
          console.log(data);

          if(!data.cookie && users.indexOf(data.name) > -1) {
          socket.emit('userExists', data.name + ' Username is already taken.');
          } else {
          users.push(data.name);
          socket.emit('userSet', {username: data.name});
          console.log(users);
          }
          });
          });





          share|improve this answer


























          • This gives me reference errors when trying to set the name, "Uncaught ReferenceError: setUsername is not defined at HTMLButtonElement.onclick " and "(index):291 Uncaught ReferenceError: checkCookie is not defined at onload " also it says that somewhere in the setUsername function there's a missing ) I can't find it though.

            – Veraen
            Nov 30 '18 at 10:05











          • SyntaxErrors are fixed now. My fault. :)

            – J. Sadi
            Nov 30 '18 at 10:09











          • ah :), I tried after syntax fixes and this actually works, 1 more thing, where in there could I give a message that would say that the name is already in use?

            – Veraen
            Nov 30 '18 at 10:10






          • 1





            Tried it just now, this works, I don't see any more issues being there so thank you very much I've been stuck on this problem for days! Thanks a huge bunch! I will award the bounty to you once it becomes available :)

            – Veraen
            Nov 30 '18 at 10:54








          • 1





            you're welcome :)

            – J. Sadi
            Nov 30 '18 at 10:55














          1





          +100







          1





          +100



          1




          +100





          You could get this done triggering your 'setUsername' using an object {name: string, cookie: boolean} instead of the username only, like i did here:



          function setUsername() {
          var textbox = document.getElementById("name");
          var txtboxval = textbox.value;
          if(textbox.value.length <= 20 && textbox.value.length >= 4){
          socket.emit('setUsername', {name: document.getElementById('name').value, cookie: false});
          setCookie("username", txtboxval, 30);
          socket.on('userExists', function(data) {
          // ########## you could add your 'already taken'-message here. :) ##########
          console.log(data);
          document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
          // this 'deletes' the cookie.
          });
          } else {
          alert("The name has to be at least 4 characters long");
          return false;
          }
          }




          function setUsername2() {
          var nimi = getCookie('username');
          socket.emit('setUsername', {name: nimi, cookie: true});
          }

          function checkCookie() {
          var user = getCookie("username");
          if (user != "") {
          alert("Welcome again " + user);
          setUsername2();
          document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Write your message here">
          <button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
          <div id = "message-container"></div></div>';
          }
          }




          users = ;
          io.on('connection', function(socket){
          console.log('an user connected');
          console.log(users);
          socket.on('setUsername', function(data) {
          console.log(data);

          if(!data.cookie && users.indexOf(data.name) > -1) {
          socket.emit('userExists', data.name + ' Username is already taken.');
          } else {
          users.push(data.name);
          socket.emit('userSet', {username: data.name});
          console.log(users);
          }
          });
          });





          share|improve this answer















          You could get this done triggering your 'setUsername' using an object {name: string, cookie: boolean} instead of the username only, like i did here:



          function setUsername() {
          var textbox = document.getElementById("name");
          var txtboxval = textbox.value;
          if(textbox.value.length <= 20 && textbox.value.length >= 4){
          socket.emit('setUsername', {name: document.getElementById('name').value, cookie: false});
          setCookie("username", txtboxval, 30);
          socket.on('userExists', function(data) {
          // ########## you could add your 'already taken'-message here. :) ##########
          console.log(data);
          document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
          // this 'deletes' the cookie.
          });
          } else {
          alert("The name has to be at least 4 characters long");
          return false;
          }
          }




          function setUsername2() {
          var nimi = getCookie('username');
          socket.emit('setUsername', {name: nimi, cookie: true});
          }

          function checkCookie() {
          var user = getCookie("username");
          if (user != "") {
          alert("Welcome again " + user);
          setUsername2();
          document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Write your message here">
          <button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
          <div id = "message-container"></div></div>';
          }
          }




          users = ;
          io.on('connection', function(socket){
          console.log('an user connected');
          console.log(users);
          socket.on('setUsername', function(data) {
          console.log(data);

          if(!data.cookie && users.indexOf(data.name) > -1) {
          socket.emit('userExists', data.name + ' Username is already taken.');
          } else {
          users.push(data.name);
          socket.emit('userSet', {username: data.name});
          console.log(users);
          }
          });
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 30 '18 at 10:51

























          answered Nov 30 '18 at 9:58









          J. SadiJ. Sadi

          1,66011023




          1,66011023













          • This gives me reference errors when trying to set the name, "Uncaught ReferenceError: setUsername is not defined at HTMLButtonElement.onclick " and "(index):291 Uncaught ReferenceError: checkCookie is not defined at onload " also it says that somewhere in the setUsername function there's a missing ) I can't find it though.

            – Veraen
            Nov 30 '18 at 10:05











          • SyntaxErrors are fixed now. My fault. :)

            – J. Sadi
            Nov 30 '18 at 10:09











          • ah :), I tried after syntax fixes and this actually works, 1 more thing, where in there could I give a message that would say that the name is already in use?

            – Veraen
            Nov 30 '18 at 10:10






          • 1





            Tried it just now, this works, I don't see any more issues being there so thank you very much I've been stuck on this problem for days! Thanks a huge bunch! I will award the bounty to you once it becomes available :)

            – Veraen
            Nov 30 '18 at 10:54








          • 1





            you're welcome :)

            – J. Sadi
            Nov 30 '18 at 10:55



















          • This gives me reference errors when trying to set the name, "Uncaught ReferenceError: setUsername is not defined at HTMLButtonElement.onclick " and "(index):291 Uncaught ReferenceError: checkCookie is not defined at onload " also it says that somewhere in the setUsername function there's a missing ) I can't find it though.

            – Veraen
            Nov 30 '18 at 10:05











          • SyntaxErrors are fixed now. My fault. :)

            – J. Sadi
            Nov 30 '18 at 10:09











          • ah :), I tried after syntax fixes and this actually works, 1 more thing, where in there could I give a message that would say that the name is already in use?

            – Veraen
            Nov 30 '18 at 10:10






          • 1





            Tried it just now, this works, I don't see any more issues being there so thank you very much I've been stuck on this problem for days! Thanks a huge bunch! I will award the bounty to you once it becomes available :)

            – Veraen
            Nov 30 '18 at 10:54








          • 1





            you're welcome :)

            – J. Sadi
            Nov 30 '18 at 10:55

















          This gives me reference errors when trying to set the name, "Uncaught ReferenceError: setUsername is not defined at HTMLButtonElement.onclick " and "(index):291 Uncaught ReferenceError: checkCookie is not defined at onload " also it says that somewhere in the setUsername function there's a missing ) I can't find it though.

          – Veraen
          Nov 30 '18 at 10:05





          This gives me reference errors when trying to set the name, "Uncaught ReferenceError: setUsername is not defined at HTMLButtonElement.onclick " and "(index):291 Uncaught ReferenceError: checkCookie is not defined at onload " also it says that somewhere in the setUsername function there's a missing ) I can't find it though.

          – Veraen
          Nov 30 '18 at 10:05













          SyntaxErrors are fixed now. My fault. :)

          – J. Sadi
          Nov 30 '18 at 10:09





          SyntaxErrors are fixed now. My fault. :)

          – J. Sadi
          Nov 30 '18 at 10:09













          ah :), I tried after syntax fixes and this actually works, 1 more thing, where in there could I give a message that would say that the name is already in use?

          – Veraen
          Nov 30 '18 at 10:10





          ah :), I tried after syntax fixes and this actually works, 1 more thing, where in there could I give a message that would say that the name is already in use?

          – Veraen
          Nov 30 '18 at 10:10




          1




          1





          Tried it just now, this works, I don't see any more issues being there so thank you very much I've been stuck on this problem for days! Thanks a huge bunch! I will award the bounty to you once it becomes available :)

          – Veraen
          Nov 30 '18 at 10:54







          Tried it just now, this works, I don't see any more issues being there so thank you very much I've been stuck on this problem for days! Thanks a huge bunch! I will award the bounty to you once it becomes available :)

          – Veraen
          Nov 30 '18 at 10:54






          1




          1





          you're welcome :)

          – J. Sadi
          Nov 30 '18 at 10:55





          you're welcome :)

          – J. Sadi
          Nov 30 '18 at 10:55













          0














          If i got it right, and you want to distinct two activities - setting username from cookie and setting username from input#name -, so you just have to emit userExists with some additional data, with some flag, like cookie (which will mean that you're setting the value from cookie) or user_input (which will mean that you're setting the value from the input field), like this:



          socket.emit('userSet', {username: data.name, user_input: true});





          share|improve this answer
























          • Mind giving an example on the part on how I should do it? I don't exactly understand how I would build the syntax.

            – Veraen
            Nov 30 '18 at 10:07











          • @Veraen, first part: you have 2 ways of setting cookies - from input and from cookies. Second part: you catch userSet event and extract value from it. But you can't say where did the event come from. So you have to put additional data in it, just to be able to say where did it come from: from input or from cookies. Now, when you can distinct username sources, you decide if you want to emit userExists or no. That's it!

            – lucifer63
            Nov 30 '18 at 10:12











          • Thank you for the answer! But I've got my solution from J. Sadi's answer.

            – Veraen
            Nov 30 '18 at 10:55
















          0














          If i got it right, and you want to distinct two activities - setting username from cookie and setting username from input#name -, so you just have to emit userExists with some additional data, with some flag, like cookie (which will mean that you're setting the value from cookie) or user_input (which will mean that you're setting the value from the input field), like this:



          socket.emit('userSet', {username: data.name, user_input: true});





          share|improve this answer
























          • Mind giving an example on the part on how I should do it? I don't exactly understand how I would build the syntax.

            – Veraen
            Nov 30 '18 at 10:07











          • @Veraen, first part: you have 2 ways of setting cookies - from input and from cookies. Second part: you catch userSet event and extract value from it. But you can't say where did the event come from. So you have to put additional data in it, just to be able to say where did it come from: from input or from cookies. Now, when you can distinct username sources, you decide if you want to emit userExists or no. That's it!

            – lucifer63
            Nov 30 '18 at 10:12











          • Thank you for the answer! But I've got my solution from J. Sadi's answer.

            – Veraen
            Nov 30 '18 at 10:55














          0












          0








          0







          If i got it right, and you want to distinct two activities - setting username from cookie and setting username from input#name -, so you just have to emit userExists with some additional data, with some flag, like cookie (which will mean that you're setting the value from cookie) or user_input (which will mean that you're setting the value from the input field), like this:



          socket.emit('userSet', {username: data.name, user_input: true});





          share|improve this answer













          If i got it right, and you want to distinct two activities - setting username from cookie and setting username from input#name -, so you just have to emit userExists with some additional data, with some flag, like cookie (which will mean that you're setting the value from cookie) or user_input (which will mean that you're setting the value from the input field), like this:



          socket.emit('userSet', {username: data.name, user_input: true});






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 30 '18 at 10:00









          lucifer63lucifer63

          16316




          16316













          • Mind giving an example on the part on how I should do it? I don't exactly understand how I would build the syntax.

            – Veraen
            Nov 30 '18 at 10:07











          • @Veraen, first part: you have 2 ways of setting cookies - from input and from cookies. Second part: you catch userSet event and extract value from it. But you can't say where did the event come from. So you have to put additional data in it, just to be able to say where did it come from: from input or from cookies. Now, when you can distinct username sources, you decide if you want to emit userExists or no. That's it!

            – lucifer63
            Nov 30 '18 at 10:12











          • Thank you for the answer! But I've got my solution from J. Sadi's answer.

            – Veraen
            Nov 30 '18 at 10:55



















          • Mind giving an example on the part on how I should do it? I don't exactly understand how I would build the syntax.

            – Veraen
            Nov 30 '18 at 10:07











          • @Veraen, first part: you have 2 ways of setting cookies - from input and from cookies. Second part: you catch userSet event and extract value from it. But you can't say where did the event come from. So you have to put additional data in it, just to be able to say where did it come from: from input or from cookies. Now, when you can distinct username sources, you decide if you want to emit userExists or no. That's it!

            – lucifer63
            Nov 30 '18 at 10:12











          • Thank you for the answer! But I've got my solution from J. Sadi's answer.

            – Veraen
            Nov 30 '18 at 10:55

















          Mind giving an example on the part on how I should do it? I don't exactly understand how I would build the syntax.

          – Veraen
          Nov 30 '18 at 10:07





          Mind giving an example on the part on how I should do it? I don't exactly understand how I would build the syntax.

          – Veraen
          Nov 30 '18 at 10:07













          @Veraen, first part: you have 2 ways of setting cookies - from input and from cookies. Second part: you catch userSet event and extract value from it. But you can't say where did the event come from. So you have to put additional data in it, just to be able to say where did it come from: from input or from cookies. Now, when you can distinct username sources, you decide if you want to emit userExists or no. That's it!

          – lucifer63
          Nov 30 '18 at 10:12





          @Veraen, first part: you have 2 ways of setting cookies - from input and from cookies. Second part: you catch userSet event and extract value from it. But you can't say where did the event come from. So you have to put additional data in it, just to be able to say where did it come from: from input or from cookies. Now, when you can distinct username sources, you decide if you want to emit userExists or no. That's it!

          – lucifer63
          Nov 30 '18 at 10:12













          Thank you for the answer! But I've got my solution from J. Sadi's answer.

          – Veraen
          Nov 30 '18 at 10:55





          Thank you for the answer! But I've got my solution from J. Sadi's answer.

          – Veraen
          Nov 30 '18 at 10:55


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516191%2fcheck-array-for-duplicate-values-if-cookies-arent-set-dont-check-if-cookies-a%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)