<script type="text/javascript"> // 创建cookie 修改cookie 指定过期时间 document.cookie = "username=Tom;expires=Thu, 18 Dec 2013 12:00:00 GMT;path=/"; //读取cookie的值 var readcookievalue = document.cookie; //删除cookie 不必写cookie的值,指定过去的时间即可 document.cookie = "username=;expires=Thu, 18 Dec 2013 12:00:00 GMT;"; //函数设置cookie的值 function setcookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000); var expires = "expires=" + d.toGMTString(); document.cookie = cname + "=" + cvalue + ";" + expires; } //函数获取cookie function getcookie(cname) { var name = cname + "="; var ca = document.cookie.split(";"); for (var i = 0; i < ca.length; i++) { var c = ca[i].trim(); if (c.indexOf(name) == 0) return c.substring(name.length, c.length); } return ""; } //检查cookie操作 function checkcookie() { var user = getcookie("username"); if (user != "") { alert("welcome again:" + user); } else { user = prompt("please enter your name:", ""); if (user != "" && user != null) { setcookie("username", user, 365); } } } </script>
时间: 2024-10-12 21:34:25