DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Javascript

Deal with JavaScript and Cookies

Share

JavaScript cookie is the most efficient method of tracking and remembering preferences, lets you store user information in small text files on computer.

By definition, a cookie is a nothing but a piece of data that is sent from a website and stored locally by the user’s browser.

Basically, web browsers and servers use HTTP protocol for communicating and we know that HTTP is a stateless protocol. That means HTTP itself has no way to keep tracking of a user’s old activities.

So, cookies are used to remember any information about the user.

For example, when any user visits a web page, his name can be stored in a cookie so that, next time when the user visits the page, the cookie “remembers” his name.

Cookies value are saved in name=value pairs: name=Jane

Cookies consists of plain text data record of variable length fields: Expires, Domain, Path, Secure and Name=Value.

How It Works ?

Visitor’s browser gets data from server in the form of a cookie. If browser accept cookie then it is stored as plain text on the visitor’s computer. Then, when visitor arrives other page on the site, browser sends same cookie for retrieval to the server.

Read Also: jQuery Cookie

Create a Cookie with JavaScript

Cookie can be created like below:

document.cookie=”username=Jane Doe”;

By default, cookie sets the path to the current page but, you can also change the path.

document.cookie=”username=Jane Doe; expires=Thu, 24 Mar 2016 23:26:45 UTC; path=/mydir”;

Expires attribute is optional but, you can provide a valid date or time.

Reading Cookies

Same as writing one, you can use document.cookie object for reading.

var x = document.cookie;
cookie_arr = x.split(';');
for(var i=0; i<cookie_arr.length; i++){
name = cookie_arr[i].split('=')[0];
value = cookie_arr[i].split('=')[1];
document.write ("Key : " + name + " - Value : " + value);
}

Modify a Cookie

You can change a cookie in the same way you created. Here the old cookie will be overwritten.

document.cookie="username=Jane Doe; expires=Thu, 26 Mar 2016 23:26:45 UTC; path=/";

Deleting a Cookie

For deleting a cookie, you need to change the expiry date to a passed date.

document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC";

No need to specify the cookie value while deleting.

Sometimes, cookies are used in the web application to identify a user’s authenticated session. So, by stealing cookie from web application, anyone can lead to hijack the authenticated user’s session.

Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

Recent Posts

Understanding Battery Coating: Key Factors to Perfection

Battery coating is the process of applying uniform layers of active materials—such as cathode and…

1 hour ago

Edge Computing vs Cloud Computing: What’s the Difference?

Let’s face it. Tech buzzwords get thrown around a lot—especially when it comes to how…

3 days ago

How Data Intelligence Shapes the Future of Digital Innovation

In today’s digital world, the boundaries between technology, finance, and innovation are rapidly disappearing. Businesses…

6 days ago

How To Power Your Backyard Parties with A Solar Power Battery?

Backyard gatherings like BBQs, family reunions, and garden parties are an exciting way to enjoy…

7 days ago

5 Best Practices for Programmatic Advertising

Marketers are always on the lookout for more effective ways to reach their target audiences.…

1 week ago

Are We All Addicted to Stimulation? The New Face of Anxiety Disorders

Does your phone control your mind more than you control your phone? Modern life exploits…

1 week ago