How can I list all cookies on the current page with JavaScript?

JavascriptWeb DevelopmentFront End Technology

While creating a cookie, use the “path” parameter. The path to the directory or web page set the cookie. This may be blank if you want to retrieve the cookie from the current page. By default, the cookie belongs to the current page.

Example

To list all cookies for the current page, try the following code −

Live Demo

<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>
      <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>
raja
Published on 31-Jan-2018 12:24:35
Advertisements