Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีแสดงรายการ Cookies ทั้งหมดในหน้าปัจจุบันด้วย JavaScript


ขณะสร้าง cookie ให้ใช้พารามิเตอร์ "path" พาธไปยังไดเรกทอรีหรือเว็บเพจที่ตั้งค่า cookie อาจว่างได้หากต้องการดึง cookie จากหน้าปัจจุบัน โดยค่าเริ่มต้น cookie จะเป็นของหน้าปัจจุบัน

ตัวอย่าง

เพื่อแสดงรายการ cookies ทั้งหมดสำหรับหน้าปัจจุบัน ลองโค้ดต่อไปนี้:

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>