如何使用 JavaScript 按名稱列出所有 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>

更新於: 2020 年 1 月 9 日

190 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.