1、什么是cookiecookie是存储于访问者计算机的变量,每当同一台计算机通过浏览器清请求某个页面时,就会发送这个cookie。您可以使用 JavaScript 来创建和取回 cookie 的值。比如,当我们访问sina等邮箱时,会有一个选项提醒我们是否要保存账号和密码,如果选是,我们的账号和密码就被保存在了cookie中。
2、使用方法我们可以通过document对象的cookie属性对当前文档的cookie进行读取,创建,修改和删除操作。
3、返回cookie我们可以通过如下代码返回当前文档的cookie:document.write(document.cookie);
4、创建cookiejsp中我们可以创建一个可在 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())}c_name为姓名,value为值,expiredays为过期天数。
5、检查cookie下面函数首先会检查 document.cookie 对象中是否存有 cookie。假如 document.cookie 对象存有某些 cookie,那么会继续检查我们指定的 cookie 是否已储存。如果找到了我们要的 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 ""}