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>