<script language="javascript"src="./js/cookie.js"></script>
//取cookie值
var hrckdeptcode = Cookie("hrckdeptcode")
var hrckdeptname = Cookie("hrckdeptname")
//保存cookie值
setCookie("hrckdeptcode", deptcode, expdate, "/", null, false)
setCookie("hrckdeptname", deptname, expdate, "/", null, false)
把下面的脚本放到 cookie.js 文件里
var expdate= new Date()
function getCookieVal (offset) {
var endstr = document.cookie.indexOf ("", offset)
if (endstr == -1) endstr = document.cookie.length
return unescape(document.cookie.substring(offset, endstr))
}
function Cookie (name) {
var arg = name + "="
var alen = arg.length
var clen = document.cookie.length
var i = 0
while (i <clen) {
var j = i + alen
if (document.cookie.substring(i, j) == arg) return getCookieVal (j)
i = document.cookie.indexOf(" ", i) + 1
if (i == 0) break
}
return null
}
function setCookie (name,value,expires,path,domain,secure) {
expdate.setTime(expdate.getTime() + (24*60*60*1000*365))
document.cookie = name + "=" + escape (value) +
((expires) ? "expires=" + expires.toGMTString() : "") +
((path) ? "path=" + path : "") +
((domain) ? "domain=" + domain : "") +
((secure) ? "secure" : "")
}
function deleteCookie(name) {
expdate = new Date()
expdate.setTime(expdate.getTime() - (86400 * 1000 * 1))
setCookie(name, "", expdate)
}
cookie是保存在浏览器中的,每个浏览器各自管理自己的cookie如果在浏览器中运行的js设置的值,本地环境的node是看不到的,如果是服务端的node,cookie的值是通过http请求发给服务端的,只有这种情况下可以在http请求头中读到cookie