html cookie的获取和设置
<script>
//进入页面时判断浏览器某个cookie是否存在
window.onload = function init() {
var phone = getCookie("phone");
if (phone != "" && phone != null) {
document.getElementById("report").style.display = "block";
document.getElementById("select").style.display = "none";
}
else {
document.getElementById("report").style.display = "none";
document.getElementById("select").style.display = "block";
}
}
//根据cookiename获取cookie值
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=")
if (c_start != -1) {
c_start = c_start + c_name.length + 1
c_end = document.cookie.indexOf(";", c_start)
if (c_end == -1) c_end = document.cookie.length
return unescape(document.cookie.substring(c_start, c_end))
}
}
return ""
}
//设置cookie的名称、值、有效期
function setCookie(c_name, value, expiredays) {
var exdate = new Date()
exdate.setDate(exdate.getDate() + expiredays)
document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}
</script>

本文介绍了一种在HTML中实现Cookie的设置与获取的方法。通过JavaScript代码,可以实现在用户浏览器上设置Cookie,并在需要时读取这些Cookie的值。文章提供了具体的函数示例,包括如何检查特定Cookie的存在、获取其值以及设置新的Cookie。
2445

被折叠的 条评论
为什么被折叠?



