javascript的cookie相关函数
JavaScript #cookie #函数2012-05-09 16:36
/** ********************************************* */ /* Cookie yige.org */ /** ********************************************* */ // 新建cookie。 function setCookie(name, value, hours, path) { var name = encodeURIComponent(name); var value = encodeURIComponent(value); var expires = new Date(); expires.setTime(expires.getTime() + hours * 3600000); path = path == "" ? "" : ";path=" + path; _expires = (typeof hours) == "string" ? "" : ";expires=" + expires.toUTCString(); document.cookie = name + "=" + value + _expires + path; } // 获取cookie值 function getCookieValue(name) { var name = encodeURIComponent(name); var allcookies = document.cookie; name += "="; var pos = allcookies.indexOf(name); if (pos != -1) { var start = pos + name.length; var end = allcookies.indexOf(";", start); if (end == -1) end = allcookies.length; var value = allcookies.substring(start, end); return decodeURIComponent(value); } else return ""; } // 删除cookie function deleteCookie(name, path) { var name = encodeURIComponent(name); var expires = new Date(0); path = path == "" ? "" : ";path=" + path; document.cookie = name + "=" + ";expires=" + expires.toUTCString() + path; }
相关文章
- Document对象详细介绍 2012/05/01
- js判断页面是否加载完成的方法 2012/04/30
- IE6浏览网页出现已终止操作的错误提示解决方法 2012/04/28
- js生成随机数 2012/04/25
- JavaScript base64_decode 函数 2012/04/17
- JavaScript base64_encode 函数 2012/04/17
- javascript7种方式刷新 2012/04/14
- javascript取得radio值 2012/04/14