JavaScript Notes: Cookies| PDF | B.Tech
top of page

JavaScript Class Notes: Cookies

Updated: Oct 16, 2022

Mobiprep has created last-minute notes for all topics of JavaScript to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on JavaScript.

Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.

JavaScript Notes: Cookies


1) What are Cookies?

Cookies are text files used to identify your computer as you use a computer network with small pieces of data like a username and password. HTTP cookies are used to identify particular users and improve web browsing experience.


 

2) How can Cookies be created in JavaScript?

Step 1. By using document. cookie a cookie can be created:

document.cookie="name=value";  

You have to give it three bits of information when calling createCookie() which is the name and value of the cookie and the number of days it is to remain active.

Step 2. The name-value pair should become ppkcookie=test cookie in this case and should be active for 7 days.

createCookie('ppkcookie','testcookie',7)

The cookie is trashed if you set the number of days to 0 when the user closes the browser. The cookie is trashed immediately if you set the days to a negative number .

Step 3) The function starts doing its job when arguments are received.

function createCookie(name, value, days) {

  • If day value is present then only we need to do the time calculation. ( if (days) { )

  • If create a new Date object containing the current date is present . ( var date = new Date(); )

Add the required number of days (in milliseconds) to the current time in milliseconds . Once the value is set the time of the date to this new value, date will be in milliseconds so now cookie should expire in that time.


date.setTime(date.getTime()+(days*24*60*60*1000));

Step 4) The variable expires should set to this date in the UTC/GMT format required by cookies.


var expires = "; expires="+date.toGMTString();

Expires is not set and the cookie expires when the user closes his browser,if 0 is passed to the function.


else var expires = "";

Step 5) At last write the new cookie into document.cookie in the correct syntax.



document.cookie = name+"="+value+expires+"; path=/";
}

 

3) How can Cookies be read using JavaScript?

Answer :

Step 1) Call the following function and pass the name of the cookie to read the cookies. Put the name in a variable. Check if this variable has a value (if the cookie does not exist the variable becomes null, which might upset the rest of your function), then do whatever is necessary.

var x = readCookie('ppkcookie1')
if (x) {
               [do something with x]
}


Step 2) The function receives the argument and starts.


function readCookie(name) {


Step 3) We're going to search for the name of the cookie, followed by an =. Create new string and assign it in nameEQ:


               var nameEQ = name + "=";


Step 4) Then split document.cookie on semicolons. Now ca is an array having all cookies which are set for this domain and path.


var ca = document.cookie.split(';');


Step 5) Then we go through all cookies i. e. nothing but array.


for(var i=0;i < ca.length;i++) {
 c should be set to the cookie to be checked.
var c = ca[i];


Step 6) If the first character is a space, remove it by using the substring() method. When the first character is not a space stop doing this.


while (c.charAt(0)==' ') c = c.substring(1,c.length);

Step 7) Now string c starts with the name of the current cookie. If this is the name of the cookie you are looking for


if (c.indexOf(nameEQ) == 0)

we've found the desired cookie .


Step 8) Now return the value of the cookie, which is the part of c that comes after nameEQ. We also end the function by returning this value.


if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
               }


Step 9) We return null after we haven't found the name we're looking for in entire array, then cookie is not present


return null;
}

 


4) How can Cookies be deleted using JavaScript?

Answer :

Cookies can be deleted using erase. Erasing is extremely simple.

eraseCookie('ppkcookie')

Name of the cookie should be passed which you want to be erased

function eraseCookie(name) {
and to set the cookie with an expiry date of one day ago call createCookie().createCookie(name,"",-1);
}

The browser, detects that the expiry date has passed, then it removes the cookie.


5) What is the time a cookie can last?

An arbitrary expiry time of cookie can be set at the time cookie is generated. By default it is active till the current browser window is open.



Top 40 Javascript Interview Questions and Answers
.pdf
Download PDF • 1.15MB


 

Mobiprep is a one stop solution for all exam preparation needs. Get all exam prep resources at a single platform.
Free Practice Tests
Top Placement Interview Questions
Top Placement MCQ's Questions
 


158 views0 comments

Recent Posts

See All
bottom of page