ReasonJun

javascript : Web APIs (cookie) 본문

Frontend/Javasciprt

javascript : Web APIs (cookie)

ReasonJun 2023. 6. 9. 00:15
728x90

cookie

In web development, a cookie is a small piece of data stored by a website in the user's web browser. Cookies are commonly used to store information that can be retrieved and used by the website or other websites that recognize the cookie. They are primarily used for session management, personalization, tracking, and maintaining user preferences.

Here are some key points about cookies:

  1. Storage: Cookies are stored as text files on the user's device. Each cookie contains a key-value pair and additional metadata such as expiration date, domain, and path.
  2. Sent with HTTP requests: Cookies are automatically sent to the server with every HTTP request made to the website that set the cookie. This allows the server to retrieve and use the information stored in the cookie.
  3. Expiration: Cookies have an expiration date or duration after which they are automatically removed from the user's browser. Some cookies are set to expire when the browser session ends, known as session cookies, while others may have a specific expiration date.
  4. Domain and path: Cookies can be restricted to specific domains and paths. By setting these properties, a cookie can be made accessible to a specific website or a subset of pages within a website.
  5. Security and privacy: Cookies are associated with security and privacy concerns. They can be used for tracking user behavior across multiple websites, and their misuse can lead to privacy issues. To address these concerns, web browsers provide settings to control cookie behavior, such as blocking or deleting cookies.
  6. HTTP-only and secure cookies: HTTP-only cookies can only be accessed by the server, not by client-side JavaScript, which helps protect against cross-site scripting (XSS) attacks. Secure cookies are only sent over HTTPS connections, ensuring the cookie data is encrypted during transmission.

In JavaScript, you can work with cookies using the document.cookie property. It allows you to read, modify, and create cookies. However, manipulating cookies directly with JavaScript can be cumbersome, and there are libraries and frameworks available that provide more convenient cookie management APIs.

It's important to note that cookies have certain limitations, such as their size limit (typically around 4KB) and the fact that they are sent with every request, which can impact network traffic. Additionally, modern web development often uses alternative mechanisms such as server-side sessions, JSON Web Tokens (JWT), or client-side storage options like Web Storage (localStorage, sessionStorage) or IndexedDB for storing and managing user data.

Overall, cookies play a significant role in web development for various purposes, but it's essential to use them responsibly and consider user privacy and security concerns.

 

  1. Reading Cookies: To read the value of a cookie, access the document.cookie property. It returns a semicolon-separated string containing all the cookies associated with the current document. You can then parse and extract the value you need.
const cookies = document.cookie;
console.log(cookies); // Output: "cookie1=value1; cookie2=value2; ..."
  1. Creating a Cookie: To create a new cookie, assign a string value to the document.cookie property. The string should be formatted as "cookieName=cookieValue". You can also set additional options like expires, path, domain, and secure as part of the cookie string.
document.cookie = "username=John Doe";
  1. Modifying a Cookie: To modify an existing cookie, simply assign a new value to the cookie using the same name.
document.cookie = "username=Jane Smith";
  1. Setting Cookie Options: To set additional options for a cookie, such as expiration date, path, domain, or secure flag, you include them as part of the cookie string.
document.cookie = "username=John Doe; expires=Fri, 31 Dec 2023 23:59:59 GMT; path=/; domain=example.com; secure";
  1. Deleting a Cookie: To delete a cookie, set its expiration date to a past date.
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=example.com";
728x90

'Frontend > Javasciprt' 카테고리의 다른 글

javascript : Web API (history)  (0) 2023.06.09
javascript : Web APIs (storage)  (0) 2023.06.09
javascript : Web APIs (console)  (0) 2023.06.09
javascript : symbol  (0) 2023.06.09
javascript : map  (0) 2023.06.09
Comments