How do cookies affect privacy?
Cookies can have both positive and negative effects on privacy:
- Positive: Cookies can enhance user experiences by remembering preferences and login information, making websites more user-friendly.
- Negative: Cookies can raise privacy concerns when they are used for tracking and profiling without user consent. Excessive tracking can result in a loss of online privacy and the potential for data misuse.
Example:To implement a cookie in JavaScript, follow these steps to set, read, and delete a user’s language cookie:
Javascript
<script> function setCookie(name, value, daysToExpire) { const date = new Date(); date.setTime(date.getTime() + (daysToExpire * 24 * 60 * 60 * 1000)); const expires = "expires=" + date.toUTCString(); document.cookie = name + "=" + value + "; " + expires; console.log(name+ " cookie created" ); } // read cookie function getCookie(name) { console.log(document.cookie); const decodedCookie = decodeURIComponent(document.cookie); const cookies = decodedCookie.split( ';' ); for (let i = 0; i < cookies.length; i++) { let cookie = cookies[i].trim(); if (cookie.indexOf(name + "=" ) === 0) { return cookie.substring(name.length + 1); } } return null ; } // delete cookie function deleteCookie(name) { document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC;" ; console.log(name+ " cookie deleted" ); } // Set a cookie setCookie( "userLanguage" , "en-US" , 30); // Read a cookie const language = getCookie( "userLanguage" ); console.log( "User's language: " + language); // Delete a cookie deleteCookie( "userLanguage" ); </script> |
Output:
Understanding Cookies in Web Browsers
The Cookie is a small message from a web server passed to the user’s browser when you visit a website. In other words, Cookies are small text files of information created/updated when visiting a website and stored on the user’s web browser. Cookies are commonly used for information about user sections, user preferences and other data on the website. Cookies help websites remember users and track their activities to provide a personalised experience.