I wanted to kick off my second blog post with a technical subject since that’s what I will be mostly writing about. For me, I spend a lot of time at my job working with all things related to browser cookies, so I wanted to share a bit about reading and writing cookies since this effects everyone. Cookies are tiny files that all web browsers place on your local computer. It stores web activity and other information that can be used to personalize you experience when on a website. Even though new tech is coming out, which I won’t get into, cookies are by far the most used. This is key for understanding browser behavior. Once you get the hang of it, you can get the read and write cookies to see and modify what kind of information is stored about you.
Let’s start by doing a simple cookie reading.
In this example let’s use Javasacript in order to read cookies. Open up Chrome(or any browser of your choice) and go ahead and press F12 to open the console. While on any website in the console, go ahead and type in ‘document.cookie’. You should immediately get a response.
If you aren’t familiar, this may be a lot of information to take in. However, you will notice the “;” symbols. Each one of these delimits a key value pairs(Key is before the = and value is after). Each key-value pair represents information stored on your computer by a website you browsed. Let’s write the following in the console in order to split each one out:
reading = document.cookie.split(‘;’); reading.forEach((ea, i) => { //space apart the key-values reading[i] = ea.replace(/=/, =
); //log into console console.log(Cookie Key-Value ${i + 1}: ${reading[i]}
); }); The results should be similar to this:
Now that you know how to read cookies, let’s take a quick moment to do cookie writing.
Again now we can take document.cookie and use ‘=’ to assign it a value. For example, write the following in your console: document.cookie = ‘writing cookies’.
You can see that we have written a new value in our cookie, “writing cookies”. This is just the beginning, you can go ahead and write all sorts of values to cookies based on different local variables and data that are available on site. You can split up values, you can retrieve others, you can split them, you can concatenate them.
When you travel form website to website, you are basically being applied several values based on your browsing behavior in order for a website to recognize you. Many of these identifiers can be unique to your browser. Common uses for this include keeping you logged in or remember what’s in your shopping basket. An example of this on a website would be:
if (key=value exists) → items in basket exist, if (key=value doesn’t exist) →empty basket.
That’s the basics. Feel free to reach out, comment or ask me about anything else on cookies, I would love to discuss it further.
Thanks for reading.