博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js的cookie操作
阅读量:7236 次
发布时间:2019-06-29

本文共 5442 字,大约阅读时间需要 18 分钟。

cookie.js:
 
//这个cookie的js代码借用的老外的,我加了中文注释--原文:http:
//www.echoecho.com/jscookies02.htm 

///设置cookie 

function setCookie(NameOfCookie, value, expiredays) 


//@参数:三个变量用来设置新的cookie: 

//cookie的名称,存储的Cookie值, 

// 以及Cookie过期的时间. 

// 这几行是把天数转换为合法的日期 


var ExpireDate = 
new Date (); 

ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000)); 


// 下面这行是用来存储cookie的,只需简单的为"document.cookie"赋值即可. 

// 注意日期通过toGMTstring()函数被转换成了GMT时间。 


document.cookie = NameOfCookie + 
"=" + escape(value) + 

  ((expiredays == 
null) ? 
"" : "; expires=" + ExpireDate.toGMTString()); 



///获取cookie值 

function getCookie(NameOfCookie) 



// 首先我们检查下cookie是否存在. 

// 如果不存在则document.cookie的长度为0 


if (document.cookie.length > 0) 



// 接着我们检查下cookie的名字是否存在于document.cookie 


// 因为不止一个cookie值存储,所以即使document.cookie的长度不为0也不能保证我们想要的名字的cookie存在 

//所以我们需要这一步看看是否有我们想要的cookie 

//如果begin的变量值得到的是-1那么说明不存在 


begin = document.cookie.indexOf(NameOfCookie+
"="); 

if (begin != -1)    



// 说明存在我们的cookie. 


begin += NameOfCookie.length+1;
//cookie值的初始位置 

end = document.cookie.indexOf(
";", begin);
//结束位置 

if (end == -1) end = document.cookie.length;
//没有;则end为字符串结束位置 

return unescape(document.cookie.substring(begin, end)); } 



return 
null


// cookie不存在返回null 



///删除cookie 

function delCookie (NameOfCookie) 


// 该函数检查下cookie是否设置,如果设置了则将过期时间调到过去的时间; 

//剩下就交给操作系统适当时间清理cookie啦 


if (getCookie(NameOfCookie)) { 

document.cookie = NameOfCookie + 
"=" + 

"; expires=Thu, 01-Jan-70 00:00:01 GMT"


}
 
演示文件cookie.html:
 
<
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
> 

<
html 
xmlns
="http://www.w3.org/1999/xhtml"
> 

<
head
> 

<
meta 
http-equiv
="Content-Type" 
content
="text/html; charset=utf-8" 
/> 

<
title
>Cookie
</title> 

<
style 
type
="text/css"
> 

#welcome h3 


font-weight:normal; 

color:#800; 


</style> 

<
script 
type
="text/javascript" 
src
="cookie.js"
>
</script> 

<
script 
type
="text/javascript"
> 


//---------------使用cookie---------------------- 

function useCookie() 


var username=getCookie("username"); 

if(username!=null){ 

document.getElementById('welcome').innerHTML="
<
h3
>欢迎您,"+username+"
</h3>"+"
<
button 
onclick
='delusr();'
>删除用户名
</button>"; 

}else{ 

var str="
<
h3
>欢迎您,游客!
</h3>"+ 

  "
<
input 
id
='usrname' 
type
='text' 
/>"+ 

  "
<
button 
id
='saveusr' 
onclick
='checksave();'
>保存用户名
</button>"; 

document.getElementById('welcome').innerHTML=str; 




function checksave() 


var el=document.getElementById('usrname'); 

if(el.value){ 

setCookie("username",el.value); 

location.reload();//刷新页面 


else 

alert("输入框不能为空"); 



function delusr() 


delCookie("username"); 

location.reload(); 


</script> 

</head> 

<
body 
onload
="useCookie();"
> 

<
div 
id
="welcome"
> 

</div> 

</body> 

</html> 

 
 
截图:
 
cookie.js:
 
//这个cookie的js代码借用的老外的,我加了中文注释--原文:http:
//www.echoecho.com/jscookies02.htm 

///设置cookie 

function setCookie(NameOfCookie, value, expiredays) 


//@参数:三个变量用来设置新的cookie: 

//cookie的名称,存储的Cookie值, 

// 以及Cookie过期的时间. 

// 这几行是把天数转换为合法的日期 


var ExpireDate = 
new Date (); 

ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000)); 


// 下面这行是用来存储cookie的,只需简单的为"document.cookie"赋值即可. 

// 注意日期通过toGMTstring()函数被转换成了GMT时间。 


document.cookie = NameOfCookie + 
"=" + escape(value) + 

  ((expiredays == 
null) ? 
"" : "; expires=" + ExpireDate.toGMTString()); 



///获取cookie值 

function getCookie(NameOfCookie) 



// 首先我们检查下cookie是否存在. 

// 如果不存在则document.cookie的长度为0 


if (document.cookie.length > 0) 



// 接着我们检查下cookie的名字是否存在于document.cookie 


// 因为不止一个cookie值存储,所以即使document.cookie的长度不为0也不能保证我们想要的名字的cookie存在 

//所以我们需要这一步看看是否有我们想要的cookie 

//如果begin的变量值得到的是-1那么说明不存在 


begin = document.cookie.indexOf(NameOfCookie+
"="); 

if (begin != -1)    



// 说明存在我们的cookie. 


begin += NameOfCookie.length+1;
//cookie值的初始位置 

end = document.cookie.indexOf(
";", begin);
//结束位置 

if (end == -1) end = document.cookie.length;
//没有;则end为字符串结束位置 

return unescape(document.cookie.substring(begin, end)); } 



return 
null


// cookie不存在返回null 



///删除cookie 

function delCookie (NameOfCookie) 


// 该函数检查下cookie是否设置,如果设置了则将过期时间调到过去的时间; 

//剩下就交给操作系统适当时间清理cookie啦 


if (getCookie(NameOfCookie)) { 

document.cookie = NameOfCookie + 
"=" + 

"; expires=Thu, 01-Jan-70 00:00:01 GMT"


}
 
演示文件cookie.html:
 
<
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
> 

<
html 
xmlns
="http://www.w3.org/1999/xhtml"
> 

<
head
> 

<
meta 
http-equiv
="Content-Type" 
content
="text/html; charset=utf-8" 
/> 

<
title
>Cookie
</title> 

<
style 
type
="text/css"
> 

#welcome h3 


font-weight:normal; 

color:#800; 


</style> 

<
script 
type
="text/javascript" 
src
="cookie.js"
>
</script> 

<
script 
type
="text/javascript"
> 


//---------------使用cookie---------------------- 

function useCookie() 


var username=getCookie("username"); 

if(username!=null){ 

document.getElementById('welcome').innerHTML="
<
h3
>欢迎您,"+username+"
</h3>"+"
<
button 
onclick
='delusr();'
>删除用户名
</button>"; 

}else{ 

var str="
<
h3
>欢迎您,游客!
</h3>"+ 

  "
<
input 
id
='usrname' 
type
='text' 
/>"+ 

  "
<
button 
id
='saveusr' 
onclick
='checksave();'
>保存用户名
</button>"; 

document.getElementById('welcome').innerHTML=str; 




function checksave() 


var el=document.getElementById('usrname'); 

if(el.value){ 

setCookie("username",el.value); 

location.reload();//刷新页面 


else 

alert("输入框不能为空"); 



function delusr() 


delCookie("username"); 

location.reload(); 


</script> 

</head> 

<
body 
onload
="useCookie();"
> 

<
div 
id
="welcome"
> 

</div> 

</body> 

</html> 

 
 
截图:
 
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/104996
,如需转载请自行联系原作者
你可能感兴趣的文章
hisi出的H264码流结构
查看>>
linux 读取物理寄存器
查看>>
信息科技时代-睿云智合Wise2C再次领航容器技术创新
查看>>
机器学习-监督学习-线性回归
查看>>
Linux c 目录操作函数scandir
查看>>
如何判断Javascript对象是否存在
查看>>
背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar
查看>>
SHOW_SPACE
查看>>
javascript如何设置DIV背景色为随机色
查看>>
【H.264/AVC视频编解码技术详解】十二、解析H.264码流的宏块结构(下):H.264帧内编码宏块的预测结构...
查看>>
【逻辑题】三个日本人
查看>>
Etu面对云计算挑战全面激发数据价值
查看>>
持久化存储对容器来说真的适合吗?
查看>>
[WCF权限控制]WCF自定义授权体系详解[原理篇]
查看>>
【译】如何应对 CNN 中的过拟合问题
查看>>
利用背景安全加强BYOD管理
查看>>
boost库的常用组件的使用(ZT)
查看>>
[转载]学习时注意思考方法——只献给初学者
查看>>
从SDN以及Docker看网络模型发生的变革
查看>>
我国云渲染农场的发展历程
查看>>