How to set JavaScript Cookie with no expiration date?


In this tutorial, we are going to learn to set a JavaScript cookie with no expiration date. Web servers deliver cookies, which are tiny files with frequently unique identifiers, to browsers. Each time your browser requests a new page, the server will receive these cookies. It allows a website to keep track of your preferences and online behavior.

The expiration option in a JavaScript cookie is optional. The “expires” attribute is optional. If you provide this attribute with a valid date or time, then the cookie will expire on a given date or time, and thereafter, the cookies' value will not be accessible.

Syntax

Now let’s move to see the syntax to set a JavaScript cookie with no expiration date −

var allcookies = document.cookie;
document.write ("All Cookies : " + allcookies );

In the syntax, by using the ‘document.cookie’ method we can get all the cookies of the document and store them in a variable. As we have above seen the expire attribute is an option and if the user does not define the time or date for expiring for them, then it will always be called.

Algorithm

We have seen the syntax above to set a JavaScript cookie with no expiration date, now we are going to see the complete approach step by step −

  • First, we will create a form in the body of the code to define some input fields in the body.

  • In the form using the button tag, we will create a button, in which we will define the onclick event.

  • The onclick event leads to invoking the function that is defined in it when the button is pressed.

  • In the script, we will define the function in which first by using the ‘document.write’ method we will get all the cookies of the page in a variable and later print them.

  • By using the split() method, we call store all the cookies in the separate indexes of an array.

  • We will traverse the array and for each index, we can get the name and value of the current cookie and can print it separately.

We have seen the syntax and algorithm to set a JavaScript cookie with no expiration date, now let’s take an example to implement the above-discussed steps.

Example

In this example, we will get all the cookies of the document and print them all together first then we will print all of them separately.

<html> <head> <script> function ReadCookie() { var allcookies = document.cookie; document.write ("All Cookies : " + allcookies ); // Get all the cookies pairs in an array cookiearray = allcookies.split(';'); // Now take key-value pair out of this array for(var i=0; i<cookiearray.length; i++) { name = cookiearray[i].split('=')[0]; value = cookiearray[i].split('=')[1]; document.write ("Key is : " + name + " and Value is : " + value); } } </script> </head> <body> <h3>How to set a javascript cookie with no expiration date</h3> <form name="myform" action=""> <p> click the following button and see the result −</p> <input type="button" value="Get Cookie" onclick="ReadCookie()"/> </form> </body> </html>

In the above code, first, we have created a form in the body of the code using the <form> tag, in which we have created a button that will call a function defined in the script of the code.

In the script, we have defined the ‘ReadCookie’ function in which we have defined a variable allcookies which will contain all the cookies of the document and that will get using the document.cookie() method.

Further, we have contained all the cookies separated in an array using the split function and later printed each of them differently. For printing them separately we traversed over the array and at each iteration printed the value and the name.

Note − Cookies have a shelf life. When the browser is closed, a cookie that has no defined expiration date will also be deleted. Because they are deleted after the browser session is over, they are sometimes referred to as session cookies (when the browser is closed). The browser will delete cookies having an expired date in the past. Setting an expiration date and storing it in the cookie will extend its lifespan past the current browser session. You may achieve this by entering a date and time in the "expires" property.

Conclusion

In this tutorial, we learned to set a JavaScript cookie with no expiration date. Web servers deliver cookies, which are tiny files with frequently unique identifiers, to browsers. Each time your browser requests a new page, the server will receive these cookies. The expiration option in a JavaScript cookie is optional. The “expires” attribute is optional. If you provide this attribute with a valid date or time, then the cookie will expire on a given date or time, and thereafter, the cookies' value will not be accessible.

Updated on: 07-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements