[英]how to read and delete a Cookie with javascript?
allCookies contents a list of my Browser Cookies. allCookies包含我的浏览器Cookies的列表。 I want to delete Cookies with this function delCookie(), but It just delete the first Cookie, and not the others cookies.
我想使用此函数delCookie()删除Cookies,但它只是删除第一个Cookie,而不是其他cookie。 And how could i do to update a 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();
}
}
It just delete the first Cookie, and not the others cookies.
它只是删除第一个Cookie,而不删除其他Cookie。
The selectedIndex
property of a <select>
element only returns the index of the first selected option. <select>
元素的selectedIndex
属性仅返回第一个选定选项的索引。 To check all of them in a multiple
select, you will need to iterate the options collection: 要检查所有的人都在一个
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;
…
}
}
And how could i do to update a cookie???
而我该怎么做才能更新Cookie?
Just overwrite the cookie, ie use the same method as when creating them. 只需覆盖cookie,即使用与创建cookie时相同的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.