A cookie is a temporary web storage file that stores keywords searched by the user on the internet or filled information for a period set by the server. It is known by various names such as Web Cookie, Browser Cookie, and HTTP Cookie.
In this article, we'll discuss how to get a cookie by name and set a cookie using JavaScript. I'm currently working on website design where I'm attempting to set a cookie, based on which I will add a reference to a JavaScript file in my HTML page.
I have an HTML page containing a list of HTML control options, with different JavaScript files as values. To accomplish this task, I've written JavaScript code to set, delete, and read cookie values by name. Let's take a look.
Session Cookie: This type of cookie automatically deletes as soon as the browser is closed. It's commonly used for government websites or when building highly secure websites.
Permanent Cookie: A permanent cookie remains in the browser even after it's closed. It stores information such as searches or form entries for a specified duration, typically 1 day, 1 month, or 1 year. These cookies are often used by travel or online booking websites.
When a website is created, cookies are generated by web developers. Once you visit such a website, it places cookies on your computer browser.
Once cookies are set in your browser, they function similarly to cache memory. They store information about your internet activities and send this data to the website's server.
In this part, I will explain the following points:
<!DOCTYPE html>
<html>
<head>
<title>DotNetPeTips</title>
<meta charset="utf-8" />
</head>
<body>
<fieldset>
<legend>Create Cookie</legend>
<button type="button" onclick="CreateCookie('testcookie',10,'testing cookie message')">Create Cookie</button>
<button type="button" onclick="getCookie('testcookie')">Read Cookie</button>
<button type="button" onclick="DeleteCookie('testcookie')">Delete Cookie</button>
</fieldset>
<script type="text/javascript">
function SetCookie(cookie_name, days,value) {
debugger;
var today = new Date();
var expiresdate = new Date(today.setTime(today.getTime() + days * 86400000));
document.cookie = cookie_name + "=" + value + "; expires=" +
expiresdate.toGMTString();
alert("cookie set successfully!");
}
function getCookie(cookie_name) {
if (document.cookie.length > 0) {
cookie_start = document.cookie.indexOf(cookie_name);
if (cookie_start) {
cookie_start = cookie_start + cookie_name.length + 1;
cookie_end = document.cookie.indexOf(";", cookie_start);
if (cookie_end == -1) {
cookie_end = document.cookie.length;
}
alert(unescape(document.cookie.substring(cookie_start, cookie_end)));
return unescape(document.cookie.substring(cookie_start, cookie_end));
}
}
return "";
}
function DeleteCookie(cookie_name) {
debugger
SetCookie(cookie_name, '', -2)
}
</script>
</body>
</html>
These functions provide basic cookie manipulation capabilities for managing cookies in a web browser using JavaScript.
function SetCookie(cookie_name, days,value) {
debugger;
var today = new Date();
var expiresdate = new Date(today.setTime(today.getTime() + days * 86400000));
document.cookie = cookie_name + "=" + value + "; expires=" +
expiresdate.toGMTString();
alert("cookie set successfully!");
}
Reading a cookie value very simple,the value of the document.cookie object is the cookie.
function getCookie(cookie_name) {
if (document.cookie.length > 0) {
cookie_start = document.cookie.indexOf(cookie_name);
if (cookie_start) {
cookie_start = cookie_start + cookie_name.length + 1;
cookie_end = document.cookie.indexOf(";", cookie_start);
if (cookie_end == -1) {
cookie_end = document.cookie.length;
}
alert(unescape(document.cookie.substring(cookie_start, cookie_end)));
return unescape(document.cookie.substring(cookie_start, cookie_end));
}
}
return "";
}
If you want to delete a cookie then you just need to set the expiry date to a time in the past.
function DeleteCookie(cookie_name) {
debugger
SetCookie(cookie_name, '', -2)
}
Whenever you use a browser, certain information from your phone or computer is stored as a file. This information includes the keywords you've used and the websites you've visited. These details are saved in a file on our mobile devices or computers so they can be accessed later.
When we search using a browser, small files are stored on our devices. Additionally, whenever we visit a website or perform a search, data is saved in a file. This data is then used to personalize our browsing experience. For example, we might see advertisements related to our past searches or interests. This is possible because a file called cookies is saved on our devices, and advertisements are tailored based on its contents.
Cookies are easy to use and implement. They require minimal data storage on the server, which is a significant advantage. Furthermore, cookies allow us to reopen previously opened tabs if our browser is closed unexpectedly. This feature saves time and data.
Cookies are specific to individual browsers. For instance, cookies saved in the Chrome browser cannot be used in other browsers like Internet Explorer or Firefox. Additionally, cookies are domain-dependent, meaning data collected from one domain cannot be accessed by another domain. The size of a cookies file is typically around 4KB, although it may vary in different browsers.