Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
William J. Terrell
17,403 PointsCookies!
We are getting ready to write a JavaScript cookie for a portal on our University's website. The goal is to have it set up to where, when the student logs in, the login page collects their username and identifies their major based on that. Then, when they reach a certain page in the portal, the cookie presents them with a link that sends them to a new page containing information specific to their major.
I've never done anything with web cookies before, so I thought I would ask and see if there is anything I should be aware of. Any insight would be greatly appreciated.
I'm also curious about the implementation; I assume a JS cookie is something I set on the page itself (I want a cookie to provide the option to navigate from Page A to Page B, so I assume I set it on Page A), but like I said, I've never done anything with them...
Thanks!
1 Answer
Luke Pettway
16,593 PointsThe cookie itself is nothing more than a datastore of information. It provides no functionality out of the box. Your code will interact with the cookie by reading it and then based off the data it will do something. Since cookies are key value pairs of strings, you will have to get creative in how your store and access that data.
For instance, when a user reaches that page, you can have a function that checks for the cookie, reads it, and based on the information in it, can then output a link to the page they should go to.
Remember to be careful with what you store in the cookie itself, as stealing a cookie means stealing the identity of that user on the website. If necessary you should encrypt anything remotely sensitive inside of it, or avoid putting it in a cookie altogether opting instead to use server side session handling.