如何使用 JavaScript 列出当前页面上的所有 cookie?
javascriptweb developmentfront end technology
创建 cookie 时,使用"path"参数。设置 cookie 的目录或网页的路径。如果您想从当前页面检索 cookie,则该参数可以为空。默认情况下,cookie 属于当前页面。
示例
要列出当前页面的所有 cookie,请尝试以下代码 −
<html>
<head>
<script>
function ReadCookie() {
var allcookies = document.cookie;
document.write ("All Cookies : " + allcookies );
// 获取数组中的所有 cookies 对
cookiearray = allcookies.split(';');
// 现在从该数组中取出键值对
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>
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

