教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 javascript cookie操作的例子

javascript cookie操作的例子

发布时间:2015-01-17   编辑:jiaochengji.com
本文介绍下,用javascript操作cookie的一个例子,有需要的朋友参考下。

1,用javascript脚本来保存访问者的cookie值,当用户下次登录时,直接读取保存在硬盘上的cookie值,以验证是否登录。
代码:

<!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" xml:lang="en" lang="en">  
<head>  
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />  
<title>JavaScript Cookie-www.jbxue.com</title>  
<script type="text/javascript">  
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[ 
function register(name) 
{ 
var curdate = new Date(); 
curdate.setMonth(curdate.getMonth() + 9); 
cookieExpires = curdate.toUTCString(); 
final_cookie = "mycookie=" + encodeURIComponent(name) + ";expires_on = " + cookieExpires; 
document.cookie = final_cookie; 
} 
function getCookie(cookie_name) 
{ 
var search_cookie = cookie_name + "=" 
if (document.cookie.length > 0) 
{  
start_position = document.cookie.indexOf(search_cookie)  
if (start_position!= -1) 
{ 
start_position += search_cookie.length 
end_position = document.cookie.indexOf(";", start_position) 
if (end_position == -1)  
end_position = document.cookie.length 
return (decodeURIComponent(document.cookie.substring(start_position, end_position))) 
} 
} 
} 
//]]>  
</script>  
</head>  
<body>  
<h1 style="color: red">JavaScript Cookie</h1>  
<hr />  
<script type="text/javascript">  
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[ 
var username = getCookie("mycookie") 
if (username) 
{ 
document.write("Welcome Back, ", username) 
} 
if (username == null) 
{ 
document.write("You haven't been here in the last nine months...") 
document.write("When you return to this page in next nine months, "); 
document.write("your name will be displayed...with Welcome."); 
document.write('<form onsubmit = "return false">'); 
document.write('<p>Enter your name: </p>'); 
document.write('<input type="text" name="username" size="40">'); 
document.write('<input type = "button" value= "Register"'); 
document.write('onClick="register(this.form.username.value); history.go(0)">'); 
document.write('</form>'); 
} 
//]]>  
</script>  
</body>  
</html>

2,以下脚本删除保存在硬盘上的cookie数据。
代码:

<!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" xml:lang="en" lang="en">  
<head><br>  
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />  
<title>JavaScript Cookie 的例子-删除cookie--www.jbxue.com</title>  
<script type="text/javascript">  
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[ 
function register(name) 
{ 
var nowDate = new Date(); 
nowDate.setMonth(nowDate.getMonth() + 9); 
cookieExpires = nowDate.toUTCString(); 
final_cookie = "mycookie=" + encodeURIComponent(name) + ";expires_on = " + cookieExpires; 
document.cookie = final_cookie; 
} 
function getCookie(cookie_name) 
{ 
var search_cookie = cookie_name + "=" 
if (document.cookie.length > 0)  
{  
start_position = document.cookie.indexOf(search_cookie) 
if (start_position!= -1) 
{ 
start_position += search_cookie.length  
end_position = document.cookie.indexOf(";", start_position) 
if (end_position == -1)  
end_position = document.cookie.length 
return (decodeURIComponent(document.cookie.substring(start_position, end_position))) 
} 
} 
} 
//]]>  
</script>  
</head>  
<body>  
<h1 style="color: red">JavaScript Cookie - 例子</h1>  
<hr />  
<script type="text/javascript">  
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[ 
var yourname = getCookie("mycookie") 
if (yourname) 
{ 
document.write("Welcome Back, ", yourname) 
} 
if (yourname == null) 
{ 
document.write("You haven't been here in the last nine months...") 
document.write("When you return to this page in next nine months, "); 
document.write("your name will be displayed...with Welcome."); 
document.write('<form onsubmit = "return false">'); 
document.write('<p>Enter your name: </p>'); 
document.write('<input type="text" name="username" size="40">'); 
document.write('<input type = "button" value= "Register"'); 
document.write('onClick="register(this.form.username.value); history.go(0)">'); 
document.write('</form>'); 
} 
//]]>  
</script>  
</body>  
</html>

您可能感兴趣的文章:
javascript操作cookie的小例子
jQuery的Cookie插件 cookies
Cookie操作插件 jQuery.Cookie
js操作cookie详解
asp.net操作cookie的例子
javascript cookie操作指南
javascript cookie操作实例详解
js 保存与获取cookie的代码
javascript cookie操作方法解析
JavaScript操作Cookie实例

[关闭]
~ ~