All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Get All Cookies

Last Updated : Mar 11, 2024

JavaScript Get All Cookies

In this tutorial we will show you the solution of JavaScript get all cookies, mainly Cookies are used to store data inside the users computer or device.

To save data inside cookies, first we have to set cookies and save the data inside cookies and then at required time and place, we can get the data.

The methods to set cookies are different in different languages. Today we will understand how to get all cookies in JavaScript.

Step By Step Guide On JavaScript Get All Cookies :-

Cookies are just same like session but the main thing is that cookies are stored on user’s device whereas sessions are used to store data at server side.

There are many thing that make difference between them.

Now, let us see how to get all cookies in JavaScript.

Here, get all cookies mean that to revel the cookies that are currently saved in browser. Now, let us see the codes given below.

<!DOCTYPE HTML>
<html>
<head>
 <title> JavaScript Validation </title>
</head>
<body>
 <h1>
  TalkersCode – JavaScript Tutorials
 </h1>
 <h2>
  JavaScript get all cookies
 </h2>
 <p>
  Click on the button below to get all the cookies using JavaScript
 </p>
 <button onclick="getCookies()"> Click Me </button>
 <p id="result"> </p>
 <script>
 var result = document.getElementById("RESULT");
 function manuRslt() {
  var cookies = document.cookie.split(';');
  var returnRslt = '';
  for(var i = 1; i <= cookies.length; i++) {
   ret += i + ' - ' + cookies[i - 1] + "<br>";
  }
  return returnRslt;
 }
 function getCookies() {
  result.innerHTML = manuRslt();
 }
 </script>
</body>
</html>
  1. Here, above we show you an example in JavaScript.
  2. For this first of all we here create a basic structure of html. For this we use html, head, title and body with script tag. We hope you know about the basic structure of html and which html tag is used for which concept.
  3. Now, when we look inside body tag. Here, we see some headings tag and a script tag. Heading tag again is a concept of html, whereas script relates to JavaScript.
  4. JavaScript is written always inside script tag. Where script is a paired tag so it also has a closing tag. Now inside these script tag, we write our JavaScript code.
  5. Here, as we see that inside body of html. We create a button and a paragraph. On click on button our function will run and result is displayed inside the paragraph tag.
  6. Now, to understand the function we have to see the JavaScript codes which are written inside script tags. Now, here as we see that inside script we create two functions. The get cookies function called on button click whereas the manuRslt gets called by get cookies function.
  7. Our all work is done under manuRslt function as we see which uses document.cookie and for loop to get all the cookies that are saved currently in browser.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to get all cookies using JavaScript.

I hope this tutorial on JavaScript get all cookies helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪