0

I'm learning about creating cookies with Javascript and have created this experimental log in page. It works 100% but I want to be able to display the users username and password. Can this be done with cookies in Javascript? I have attached my current code below:

function displayCookies(){
    if (GetCookie("user") || GetCookie("pass")){
        document.regform.usercookie.value = GetCookie("user");
        document.regform.passcookie.value = GetCookie("pass");
    }else{
        document.cregistrationform.usercookie.value = "Make sure you have signed up!";
        document.registrationform.passcookie.value = "Make sure you have signed up!";
    }
}
function createCookies(){
    var username = document.registrationform.user.value;
    var password = document.registrationform.pass.value;
    var date = Date();
    SetCookie("user", user);
    SetCookie("pass", pass);
    alert("You have registered!");
    alert("Welcome back" + username ". Your last visit was" + date);
}

function reloadPage() {
  location.reload(); 
}

</script>
Aaron Martin
  • 128
  • 1
  • 13
  • 1
    What if they visit from another device, or even another browser on the same device? Best to handle this *server-side*. – T.J. Crowder Mar 01 '18 at 11:54
  • If you *do* decide to do this client-side, don't use cookies. Cookies are for communicating information between the client and server, and they piggyback on every HTTP exchange. For purely client-side information, use [web storage](http://www.w3.org/TR/webstorage/). – T.J. Crowder Mar 01 '18 at 11:55
  • 1
    @T.J.Crowder I'm aware the best approach is server-side but at this point I'm not setting up a database to store username or passwords. I just want to create a cookie to display the last visit in the current browser I am working in – RollingThunder27 Mar 01 '18 at 12:02
  • 1
    Then again, don't use a cookie, use web storage. See the link above, and probably more helpfully, [the MDN page](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API). – T.J. Crowder Mar 01 '18 at 12:04
  • I don't see the "date" variable you mentioned. – floverdevel Mar 01 '18 at 12:05
  • @floverdevel I removed it because when I tried running my code to test it nothing would work at all – RollingThunder27 Mar 01 '18 at 12:08
  • Then I think you should show us the actual code that you try to run if you want us to be able to help you :) – floverdevel Mar 01 '18 at 12:19
  • @floverdevel apologies! I'll update the question. Please see the code I was trying :) – RollingThunder27 Mar 01 '18 at 12:22

1 Answers1

0

First function contains "c" in front of registration form? That should be removed:

function displayCookies(){
    if (GetCookie("username") || GetCookie("password")){
        document.registrationform.usernamecookie.value = GetCookie("username");
        document.registrationform.passwordcookie.value = GetCookie("password");
    }else{
        document.registrationform.usernamecookie.value = "Please register!";
        document.registrationform.passwordcookie.value = "Please register!";
    }
}

Duplicate question of: How do I create and read a value from cookie?

Aaron Martin
  • 128
  • 1
  • 13