Ask coding questions

← Back to all posts
Cookies
h
JacobMcPherson1 (191)

How do i save game progress with cookies?

Comments
hotnewtop
Coder100 (16853)

I would more recommend localStorage, so much easier to use, and you don't have to worry about expiration.

localStorage.setItem("<key>", "<value>");

just like that! That's it! The value has to be a string, however.

localStorage.setItem("cookies", "0");

and then to get a kei:

Number(localStorage.setItem("cookies"));

Number() just converts it into a number, which is useful for us.

JacobMcPherson1 (191)

@Coder100 so I just put both those lines in and it'll save your cookie amount?

SameeraMurthy (110)

Solution

In JavaScript, you can create cookies with document.cookie.

document.cookie = "score=10";

But if you want to use more features out of
cookies, check out
https://www.w3schools.com/js/js_cookies.asp

It also gives you some code to create a wrapper function for setting and getting cookies.
https://www.w3schools.com/js/tryit.asp?filename=tryjs_cookie_username

You can also save info with localStorage.
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
https://www.w3schools.com/html/html5_webstorage.asp