[英]how to read and delete a Cookie with javascript?
allCookies包含我的瀏覽器Cookies的列表。 我想使用此函數delCookie()刪除Cookies,但它只是刪除第一個Cookie,而不是其他cookie。 而我該怎么做才能更新Cookie?
<input type="button" value="DElete" onclick="delCookie()">
<input type="button" value="Update" onclick="modCookie()">
<select multiple id="allCookies" size="5">
<!-- Cookies content-->
</select><br>
function delCookie() {
if (confirm('Are u sure?')) {
var e = document.getElementById("allCookies");
var strUser = e.options[e.selectedIndex].value;
document.cookie = encodeURIComponent(strUser) + "=deleted; expires=" + new Date(0).toUTCString();
}
}
它只是刪除第一個Cookie,而不刪除其他Cookie。
<select>
元素的selectedIndex
屬性僅返回第一個選定選項的索引。 要檢查所有的人都在一個multiple
選擇,你會需要遍歷options集合:
var os = document.getElementById("allCookies").options;
for (var i=0; i<os.length; i++) {
if (os[i].selected) {
var strUser = os[i].value;
…
}
}
而我該怎么做才能更新Cookie?
只需覆蓋cookie,即使用與創建cookie時相同的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.