如何更新和删除Cookie? [英] How to update and delete a cookie ?
本文介绍了如何更新和删除Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助以了解如何更新值,以及如何删除从此代码创建的 Cookie !我是新来的 javascript ,所以如果有人可以帮助我很好。
I need help to know how to update values and how to delete a cookie created from this code! I'm new to javascript so ,it's great if anyone can help me.
<script type="text/javascript">
function getCookie(c_name) {
var i,x,y,ARRcookies = document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++) {
x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x = x.replace(/^\s+|\s+$/g,"");
if (x==c_name) {
return unescape(y);
}
}
}
function setCookie(c_name,value,exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function check2Cookie() {
var username=getCookie("username");
if (username!=null && username!="") {
username= "0";
setCookie("username",username,1000);
}
else {
username=" ";
if (username!=null && username!="") {
username= "0";
setCookie("username",username,1000);
}
}
}
</script>
这是创建cookie的代码。谢谢。
This is the code for cookie creation .Thank you.
创建代码 setCookie(username,username,1000);
更新此Cookie并删除此Cookie。
Now how to update this cookie and delete this cookie.
推荐答案
http://www.quirksmode.org/js/cookies.html
更新只是使用createCookie重置
update would just be resetting it using createCookie
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
这篇关于如何更新和删除Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文