zoukankan
html css js c++ java
javascript操作cookie实例
话我就不多说了,大家都能看懂,写的有点乱,帮别人写的,贴下来,以后忘了再用
代码实现功能:
1.点击登录保存cookie
2.刷新或重新打开页面读出cookie
3.点击delete 删除当前username下的cookie
没有完成的问题:
大家看到在setCookie方法里能设置许多东西,如过期时间,作用域,保存路径等,但由于对这几个参数改如何设置不太清楚,比如作用域就是页面的根域名,路径该保存成什么格式的,如果有谁知道,请告诉我哈,谢谢了
<!
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=gb2312"
/>
<
title
>
无标题文档
</
title
>
<
script
type
="text/javascript"
>
function
GetCookieVal(offset)
//
获得Cookie解码后的值
{
var
endstr
=
document.cookie.indexOf (
"
;
"
, offset);
if
(endstr
==
-
1
)
endstr
=
document.cookie.length;
return
unescape(document.cookie.substring(offset, endstr));
}
function
SetCookie(name, value)
//
设定Cookie值
{
var
expdate
=
new
Date();
var
argv
=
SetCookie.arguments;
var
argc
=
SetCookie.arguments.length;
var
expires
=
(argc
>
2
)
?
argv[
2
] :
null
;
var
path
=
(argc
>
3
)
?
argv[
3
] :
null
;
var
domain
=
(argc
>
4
)
?
argv[
4
] :
null
;
var
secure
=
(argc
>
5
)
?
argv[
5
] :
false
;
if
(expires
!=
null
) expdate.setTime(expdate.getTime()
+
( expires
*
1000
));
document.cookie
=
name
+
"
=
"
+
escape (value)
+
((expires
==
null
)
?
""
: (
"
; expires=
"
+
expdate.toGMTString()))
+
((path
==
null
)
?
""
: (
"
; path=
"
+
path))
+
((domain
==
null
)
?
""
: (
"
; domain=
"
+
domain))
+
((secure
==
true
)
?
"
; secure
"
:
""
);
}
function
DelCookie(name)
//
删除Cookie
{
var
exp
=
new
Date();
exp.setTime (exp.getTime()
-
1
);
var
cval
=
GetCookie (name);
document.cookie
=
name
+
"
=
"
+
cval
+
"
; expires=
"
+
exp.toGMTString();
}
function
GetCookie(name)
//
获得Cookie的原始值
{
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
get$(obj)
{
return
document.getElementById(obj);
}
</
script
>
</
head
>
<
body
onload
="javascript:get$('username').value=GetCookie('Page')==null?'':GetCookie('Page');get$('password').value=GetCookie(GetCookie('Page'))==null?'':GetCookie(GetCookie('Page'));"
>
<
table
width
="580"
border
="0"
align
="center"
>
<
tr
>
<
td
width
="114"
>
</
td
>
<
td
width
="456"
>
</
td
>
</
tr
>
<
tr
>
<
td
>
用户名:
</
td
>
<
td
><
input
type
="text"
name
="username"
id
="username"
/></
td
>
</
tr
>
<
tr
>
<
td
>
密码
</
td
>
<
td
><
input
type
="text"
name
="password"
id
="password"
/></
td
>
</
tr
>
<
tr
>
<
td
><
input
type
="button"
name
="Submit"
value
="登陆"
onclick
="SetCookie('Page',get$('username').value);SetCookie(get$('username').value,get$('password').value);"
/></
td
>
<
td
><
input
type
="button"
value
="delete"
onclick
="DelCookie(GetCookie('Page'));DelCookie('Page');get$('username').value='';get$('password').value='';"
/></
td
>
</
tr
>
</
table
>
</
body
>
</
html
>
第八宗罪
Tobin
查看全文
相关阅读:
2010年7月29日_周四_Working with CallbackResults
2010年7月19日_周一_Using the Common Data Source API in a Web application
2010年7月20日_周二_Accessing ArcGIS Server from a Web application
Flex代码自动换行(自动格式化)
2010年7月21日_周三_Developing a scriptable Web ADF control
2010年7月30日_周五_ASP.NET callback solution
2010年8月02日_周一_map control
SDE数据库的异地部署
2010年7月27日_周二_introduction_to_web_controls
ICE在VC中的配置
原文地址:https://www.cnblogs.com/tobin/p/1237999.html
最新文章
[面试] 在单链表末尾添加一个节点 C++ 实现
[数据结构] 数据结构与算法他人小实用汇总 【汇总】
面试题15 查找最小的 k 个元素 [数组] / (堆优化 STL O(nlogk) )[STL]
面试题13 用两个栈实现队列 【栈】
面试题14 栈的 push, pop 序列 【栈】
[C++] 关于C++ const 的全面总结
C#字节数组转换成字符串
Xamarin 2.0发布了。价格也下调了。
数组byte[]合并与讨论
jquery UBB 留言插件
热门文章
stl里list的sort()用法
PHP 脚本加密 工具(软件)
jquery DWZ富客户端框架
在windows 下搭建 Nginx 服务器
UDF to show the expression of cell formula.
JAVA基础知识汇总
如何去掉ECShop 2.7.2中的Powered by ECShop字符
翻页程序——每个合格程序员的必会程序
jquery win7弹出窗口
2010年7月28日_周三_Working with AJAX capabilities of the Web ADF_overview
Copyright © 2011-2022 走看看