如下实例在页面载入时执行 checkCookie() 函数。
Cookie 用于存储 web 页面的用户信息。web
Cookie 是一些数据, 存储于你电脑上的文本文件中。数组
当 web 服务器向浏览器发送 web 页面时,在链接关闭后,服务端不会记录用户的信息。浏览器
Cookie 的做用就是用于解决 "如何记录客户端的用户信息":服务器
Cookie 以名/值对形式存储,以下所示:cookie
username=John Doe
当浏览器从服务器上请求 web 页面时, 属于该页面的 cookie 会被添加到该请求中。服务端经过这种方式来获取用户的信息。函数
JavaScript 能够使用 document.cookie 属性来建立 、读取、及删除 cookie。spa
JavaScript 中,建立 cookie 以下所示:code
document.cookie="username=John Doe";
您还能够为 cookie 添加一个过时时间(以 UTC 或 GMT 时间)。默认状况下,cookie 在浏览器关闭时删除:blog
document.cookie="username=John Doe; expires=Thu, 18 Dec 2043 12:00:00 GMT";
您能够使用 path 参数告诉浏览器 cookie 的路径。默认状况下,cookie 属于当前页面。 ip
document.cookie="username=John Doe; expires=Thu, 18 Dec 2043 12:00:00 GMT; path=/";
在 JavaScript 中, 能够使用如下代码来读取 cookie:
var x = document.cookie;
document.cookie 将以字符串的方式返回全部的 cookie,类型格式: cookie1=value; cookie2=value; cookie3=value;
在 JavaScript 中,修改 cookie 相似于建立 cookie,以下所示:
document.cookie="username=John Smith; expires=Thu, 18 Dec 2043 12:00:00 GMT; path=/";
旧的 cookie 将被覆盖。
删除 cookie 很是简单。您只须要设置 expires 参数为之前的时间便可,以下所示,设置为 Thu, 01 Jan 1970 00:00:00 GMT:
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
document.cookie 属性看起来像一个普通的文本字符串,其实它不是。
即便您在 document.cookie 中写入一个完整的 cookie 字符串, 当您从新读取该 cookie 信息时,cookie 信息是以名/值对的形式展现的。
若是您设置了新的 cookie,旧的 cookie 不会被覆盖。 新 cookie 将添加到 document.cookie 中,因此若是您从新读取document.cookie,您将得到以下所示的数据:
cookie1=value; cookie2=value;
若是您须要查找一个指定 cookie 值,您必须建立一个JavaScript 函数在 cookie 字符串中查找 cookie 值。
在如下实例中,咱们将建立 cookie 来存储访问者名称。
首先,访问者访问 web 页面, 他将被要求填写本身的名字。该名字会存储在 cookie 中。
访问者下一次访问页面时,他会看到一个欢迎的消息。
在这个实例中咱们会建立 3 个 JavaScript 函数:
首先,咱们建立一个函数用于存储访问者的名字:
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 的名称为 cname,cookie 的值为 cvalue,并设置了 cookie 的过时时间 expires。
该函数设置了 cookie 名、cookie 值、cookie过时时间。
而后,咱们建立一个函数用户返回指定 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 名的参数为 cname。
建立一个文本变量用于检索指定 cookie :cname + "="。
使用分号来分割 document.cookie 字符串,并将分割后的字符串数组赋值给 ca (ca = document.cookie.split(';'))。
循环 ca 数组 (i=0;i<ca.length;i++),而后读取数组中的每一个值,并去除先后空格 (c=ca[i].trim())。
若是找到 cookie(c.indexOf(name) == 0),返回 cookie 的值 (c.substring(name.length,c.length)。
若是没有找到 cookie, 返回 ""。
最后,咱们能够建立一个检测 cookie 是否建立的函数。
若是设置了 cookie,将显示一个问候信息。
若是没有设置 cookie,将会显示一个弹窗用于询问访问者的名字,并调用 setCookie 函数将访问者的名字存储 365 天:
function checkCookie() { var username=getCookie("username"); if (username!="") { alert("Welcome again " + username); } else { username = prompt("Please enter your name:",""); if (username!="" && username!=null) { setCookie("username",username,365); } } }
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; } 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 ""; } function checkCookie(){ var user=getCookie("username"); if (user!=""){ alert("欢迎 " + user + " 再次访问"); } else { user = prompt("请输入你的名字:",""); if (user!="" && user!=null){ setCookie("username",user,30); } } }
如下实例在页面载入时执行 checkCookie() 函数。