如何使用JavaScript列出当前页面上的所有cookie?

创建cookie时,请使用“ path”参数。目录或网页的路径设置cookie。如果要从当前页面检索cookie,则该字段可以为空白。默认情况下,cookie属于当前页面。

示例

要列出当前页面的所有cookie,请尝试以下代码-

现场演示

<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>