JavaScript – Cookie


What is a cookie ?

Cookie is a small storage which stores a small amount of data.

This small piece of data is stored in text format mostly on the client side.

What is a cookie in javascript ?

Cookies are a small amount of data which is stored in the client side web browser in javascript.

Cookies are created when you use your browser to visit a web site that cookies are used to track our movements within the site.

What are the uses of cookies ?

When a user is working in a web browser and suddenly the connection is lost.

To get the information of the user cookies are used.

Cookies store the information of the user, so that we can retrieve the information of the user.

How do cookies work?

Suppose we are logging in the login form, after logging

1. A new request is sent to the server.

2. In Server side checks the user is old user or new user.

3. If its a new user, it creates a cookie and sends back the response in the browser to user.


4. That new cookie contains information about the new user.

5. And finally this new cookie is stored in the web browser.

Moreover, for the next time the user cookie information is displayed in the login form.

Javascript interview questions basic !!!

Click to Learn More about – Javascript online learning

1. Setting a cookie

This is the process of creating a new cookie.

Syntax :

document.cookie="key=value";

Example

<script>
	document.write("Setting a cookie name with key and value");
	document.cookie="name=Abdul Kalaam";
	document.cookie="age=87";
	document.cookie="location=tamilnadu";
</script>

2. Getting a cookie

This is the process of getting the existing cookie.

Syntax :

var someVariable = document.cookie;

Example :

 <script>  
        if(document.cookie.length!=0)  
        {  
            var array=document.cookie.split("=");  
        alert("Name="+array[0]+" "+"Value="+array[1]);  
        }  
        else  
        {  
        alert("Cookie not available");  
        }  
    </script>

Output :

3. Setting a cookie with expire day

Example :

<!DOCTYPE html>
<html>

	<head>
		<title>Our title</title>
		<script>
			document.write("Setting a cookie with expire day 5");
			setCookie('hasVisitedToday', 'Yes', 5);
		</script>
	</head>
	<body>
	</body>
</html>

4. Setting a cookie with expire day and accessible for the whole website.


Example :

<!DOCTYPE html>
<html>

	<head>
		<title>Our title</title>
		<script>
			document.write("Setting a cookie with expire day 5 and accessible along the whole website");
			setCookie('hasVisitedToday', 'Yes', 5, '/');
		</script>
	</head>
	<body>
	</body>
</html>

5. Setting a cookie with expire day and accessible for the particular folder inside the whole website.

Example :

<!DOCTYPE html>
<html>

	<head>
		<title>Our title</title>
		<script>
			document.write("Setting a cookie with expire day 5 and accessible only to some folder 'Client-Side' in the website");
			setCookie('hasVisitedToday', 'Yes', 5, '/Client-Side');
		</script>
	</head>
	<body>
	</body>
</html>

6. Setting a cookie with expire day and accessible for the particular folder and transmitted securely inside the whole website.

Example :

<!DOCTYPE html>
<html>

	<head>
		<title>Our title</title>
		<script>
			document.write("Setting a cookie with expire day 5 and accessible only to some folder 'Client-Side' along transmitted securly inside the website");
			setCookie('hasVisitedToday', 'Yes', 5, '/Client-Side','',1);
		</script>
	</head>
	<body>
	</body>
</html>

7. Setting a cookie with expire day and accessible for the particular folder and also setting a domain name.

Example :

<!DOCTYPE html>
<html>

	<head>
		<title>Our title</title>
		<script>
			document.write("Setting a cookie with expire day 5 and accessible only to some folder 'Client-Side' and setting a domain name");
			setCookie('hasVisitedToday', 'Yes', 5, '/Client-Side','cstechbook.com');
		</script>
	</head>
	<body>
	</body>
</html>

8. Setting a cookie and Deleting a cookie by setting empty value and negative expire date.

Example :

<!DOCTYPE html>
<html>

	<head>
		<title>Our title</title>
		<script>
			document.write("Setting a cookie with expire day 5");
			setCookie('hasVisitedToday', 'Yes', 5, '/');
			document.write("Deleting a cookie by setting cookie value empty and negative expire date.");
			setCookie('hasVisitedToday', '', -1, '/');
		</script>
	</head>
	<body>
	</body>
</html>