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
查看全文
相关阅读:
SQL数据类型详解
将Excel表格导入DataTable的方法
.net的反射机制
经典SQL语句大全(一)
c# Invoke和BeginInvoke 区别
c#中两种常用的异步调用方法
SQL存储过程参数问题
API 函数大全(下)
API函数大全 (上)
javascript 常用function
原文地址:https://www.cnblogs.com/tobin/p/1237999.html
最新文章
JBPM数据库分析
什么是 serialVersionUID?
JBPM Activty深入解析
JBPM中JPDL深入解析
线程同步之lock学习
JBPM之长事务设计解析
JBPM IOC实现
JBPM流程定义版本升级设计
JBPM与企业架构模式之单例模式
模块化css技巧
热门文章
常见HTTP状态(304,200等)[转载]
css实现单行文字居中,其他行居左效果
js闭包学习
js预解析学习
收藏的网站
浏览器区域高度的获取
解决ie不兼容问题
鼠标点击的热区用outline:none可以去掉
二叉排序树
如何用C#写一个透明控件?(WinForm程序)
Copyright © 2011-2022 走看看