Writing and Reading and Deleting a Cookie on the Browser
来源:互联网 发布:螺栓重量计算软件 编辑:程序博客网 时间:2024/04/30 16:57
Creating Cookies with a <META> Tag
Listing 1. Creating a Cookie that Is Valid Until a Certain Date
<HTML>
<HEAD>
<TITLE>Creating a cookie that is valid until a certain date</TITLE> <MET HTTP-EQUIV="Set-Cookie" CONTENT="userId=678;expires=Wednesday, 26-Dec-01 16:00:00 GMT path=/">
</HEAD>
<BODY>
Unless you set your browser to not accept cookies, a cookie called userId with a value o 678 has been created for you.
</BODY>
</HTML>
<HEAD>
<TITLE>Creating a cookie that is valid until a certain date</TITLE> <MET HTTP-EQUIV="Set-Cookie" CONTENT="userId=678;expires=Wednesday, 26-Dec-01 16:00:00 GMT path=/">
</HEAD>
<BODY>
Unless you set your browser to not accept cookies, a cookie called userId with a value o 678 has been created for you.
</BODY>
</HTML>
Listing 2. Creating a Cookie that Is Only Valid Until the Browser Is Closed
<HTML>
<HEAD>
<TITLE>Creating a cookie that is valid for this session only</TITLE> <MET HTTP-EQUIV="Set-Cookie" CONTENT="ProductID=2x3;">
</HEAD>
<BODY>
Unless you set your browser not to accept cookies, this page creates a cookie calle ProductID with a value of "2x3". The cookie won't be written to your hard drive. It live until the browser is closed.
</BODY>
</HTML>
<HEAD>
<TITLE>Creating a cookie that is valid for this session only</TITLE> <MET HTTP-EQUIV="Set-Cookie" CONTENT="ProductID=2x3;">
</HEAD>
<BODY>
Unless you set your browser not to accept cookies, this page creates a cookie calle ProductID with a value of "2x3". The cookie won't be written to your hard drive. It live until the browser is closed.
</BODY>
</HTML>
Creating Cookies with document.cookie
On the client side, JavaScript is used to create cookies. There are several ways to do this. The easiest way is to use document.cookie. The following is the syntax for creating a cookie this way:
document.cookie = "cookieName=cookieValue [; expires=timeInGMTString] [; path=pathName] [; domain=domainName] [; secure]"
The code in Listing 3 creates a cookie called Quantity with a value of 7 that lives as long as the browser is open.
Listing 3. Creating a Cookie with document.cookie
<HTML> <HEAD> <TITLE>Creating a cookie with document.cookie</TITLE> <SCRIPT LANGUAGE="JavaScript"> document.cookie="Quantity=7"; </SCRIPT> </HEAD> <BODY> This page creates a cookie on the client side. Make sure that your browser is set to accept cookies. </BODY> </HTML>
Writing and Reading Cookies
Listing 4. Writing and Reading Cookies
<HTML>
<HEAD>
<TITLE>Writing and Reading Cookies</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie(name) {
var cName = name + "=";
var dc = document.cookie;
if (dc.length>0) {
begin = dc.indexOf(cName);
if (begin != -1) {
begin += cName.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin,end));
}
}
return null;
}
</SCRIPT>
</HEAD>
<BODY>
Type in your user id, and then click the Create Cookie button.
A cookie will be created for you.
<BR>
<FORM>
User ID: <INPUT TYPE=TEXT NAME=UserID>
<BR>
<INPUT TYPE=BUTTON VALUE="Create Cookie"
onClick='setCookie("UserID", document.forms[0].UserID.value)'>
<BR>
Click the Read Cookie button to display the cookie.
<INPUT TYPE=BUTTON VALUE="Read Cookie"
onClick='alert(getCookie("UserID"))'>
</FORM>
</BODY>
</HTML>
<HEAD>
<TITLE>Writing and Reading Cookies</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie(name) {
var cName = name + "=";
var dc = document.cookie;
if (dc.length>0) {
begin = dc.indexOf(cName);
if (begin != -1) {
begin += cName.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin,end));
}
}
return null;
}
</SCRIPT>
</HEAD>
<BODY>
Type in your user id, and then click the Create Cookie button.
A cookie will be created for you.
<BR>
<FORM>
User ID: <INPUT TYPE=TEXT NAME=UserID>
<BR>
<INPUT TYPE=BUTTON VALUE="Create Cookie"
onClick='setCookie("UserID", document.forms[0].UserID.value)'>
<BR>
Click the Read Cookie button to display the cookie.
<INPUT TYPE=BUTTON VALUE="Read Cookie"
onClick='alert(getCookie("UserID"))'>
</FORM>
</BODY>
</HTML>
Deleting a Cookie
You don't really delete a cookie, you just make it expire by setting the expiration date to the first second of the year 1970, as in the deleteCookie function in Listing 5. Note that the function uses the getCookie function from Listing 4, so you need to paste the getCookie function in your page as well.
Listing 5. Deleting a Cookie
<SCRIPT LANGUAGE="JavaScript">
function deleteCookie (name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path==null) ? "" : "; path=" + path) +
((domain==null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
function getCookie(name) {
var cName = name + "=";
var dc = document.cookie;
if (dc.length>0) {
begin = dc.indexOf(cName);
if (begin != -1) {
begin += cName.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin,end));
}
}
return null;
}
</SCRIPT>
function deleteCookie (name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path==null) ? "" : "; path=" + path) +
((domain==null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
function getCookie(name) {
var cName = name + "=";
var dc = document.cookie;
if (dc.length>0) {
begin = dc.indexOf(cName);
if (begin != -1) {
begin += cName.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin,end));
}
}
return null;
}
</SCRIPT>
- Writing and Reading and Deleting a Cookie on the Browser
- Reading and Writing Logs
- Reading and Writing Logs
- Robust Reading and Writing
- Reading and Writing Files
- PHP Reading A File |and| Writing To A File
- 【DataStructure】A useful util class for reading and writing files
- Reading and Writing using a Large Random Access File
- Creates a pipe for reading and writing的例子程序
- 关于Thinking,Reading and Writing
- Reading and writing text files
- 1.7writing and reading files
- Reading and writing text files
- Reading and writing binary files
- Reading and writing Serializable objects
- Reading and writing text files
- reading and writing xml files
- gradle Reading and writing JSON
- “实践型”高级软件工程师精英成就计划,向年薪10W冲刺!
- 【分享】印刷企业信息管理系统分析
- 密码生成器
- nvdia芯片集成网卡问题
- 各大杀毒软件比较与引擎介绍
- Writing and Reading and Deleting a Cookie on the Browser
- SCJP 2007-06-14
- 关于本博客所有代码下载方式及说明
- null
- C#创建数据库表
- SWT中TableViewer的使用:
- SWT..table
- 过滤器
- 经典]18位身份证源代码