JavaScript Cookies

Meet the computer cookie

A computer “cookie” is more formally known as an HTTP cookie, a web cookie, an Internet cookie or a browser cookie. The name is a shorter version of “magic cookie,” which is a term for a packet of data that a computer receives and then sends back without changing or altering it.

No matter what it’s called, a computer cookie consists of information. When you visit a website, the website sends the cookie to your computer. Your computer stores it in a file located inside your web browser. (To help you find it, this file is often called “Cookies.”)

What do browser cookies do?

The purpose of the computer cookie is to help the website keep track of your visits and activity. This isn’t always a bad thing. For example, many online retailers use cookies to keep track of the items in a user’s shopping cart as they explore the site. Without cookies, your shopping cart would reset to zero every time you clicked a new link on the site. That would make it difficult to buy anything online!

A website might also use cookies to keep a record of your most recent visit or to record your login information. Many people find this useful so that they can store passwords on frequently used sites, or simply so they know what they have visited or downloaded in the past.

Different types of cookies keep track of different activities. Session cookies are used only when a person is actively navigating a website; once you leave the site, the session cookie disappears. Tracking cookies may be used to create long-term records of multiple visits to the same site. Authentication cookies track whether a user is logged in, and if so, under what name.

Are Internet cookies safe?

Under normal circumstances, cookies cannot transfer viruses or malware to your computer. Because the data in a cookie doesn’t change when it travels back and forth, it has no way to affect how your computer runs.

Create a Cookie with JavaScript

 document.cookie = "username=Pikachu"; 

Read a Cookie with JavaScript

let x = document.cookie; 

Delete a Cookie with JavaScript

document.cookie = "username=; expires=Thu, 04 Dec 2021 00:00:00 UTC;

JavaScript Cookies Practicals

Prompt user to enter his name and add it to cookies. If the user is new user then show an alert box stating Welcome <user name> otherwise display Welcome again <user name>. Cookies should get removed already after 24 hours.

FUNCTION FOR GETTING THE NAME OF LAST USER

function getCookie(cookieName) {
    let cookieValue = "";
    if (document.cookie) {
        let cookies_string = document.cookie;
        let cookieArr = cookies_string.split(';');
        for (let i = 0; i < cookieArr.length; i++) {
            let cookie = cookieArr[i].trim();
            if (cookie.startsWith(cookieName)) {
                let position = cookie.indexOf("=");
                cookieValue = cookie.slice(position + 1);
            }
        }
    }
    return cookieValue;
}

FUNCTION FOR SETTING THE COOKIE

function setCookie(cname, cvalue, exdays) {
    const day = new Date();
    day.setTime(day.getTime() + (exdays * 24 * 60 * 60 * 1000));
    let expires = "expires=" + day.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function forgetMe() {
    delCookie("lastuser");
}
let currentUser = prompt("ENTER YOUR NAME : ");
let lastuser = getCookie("lastuser");
if (lastuser == currentUser)
    alert("WELCOME AGAIN " + currentUser)
else {
    alert("WELCOME " + currentUser);
    setCookie("lastuser", currentUser);
}

Cookies store user information in web pages.

That’s it!
You have successfully completed the post.

Peace Out!

One comment

Leave a Reply

Your email address will not be published.