| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1072 人关注过本帖
标题:cookie的读写
只看楼主 加入收藏
flybenzsl500
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2008-4-10
收藏
 问题点数:0 回复次数:2 
cookie的读写
我写了一段这样的javascript代码。用来写入cookie的。请指点一下问题所在:
<script type="text/javascript">
var cookieName="login";
var cookieValue="benzsl500";
var theCookie=cookieName+"="+cookieValue;
document.cookie=theCookie;
</script>
搜索更多相关主题的帖子: cookie 
2008-04-14 23:49
渚薰
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:1132
专家分:0
注 册:2006-8-6
收藏
得分:0 
你写的很不规范,请认真阅读下面的文章(来源于《javascript权威指南》)。另外,我以前在这里上传过一个用于操作cookie的类,你自己搜索下吧。


19.2. Storing Cookies
To associate a transient cookie value with the current document, simply set the cookie property to a string of the form:

name=value



For example:

document.cookie = "version=" + encodeURIComponent(document.lastModified);



The next time you read the cookie property, the name/value pair you stored is included in the list of cookies for the document. Cookie values may not include semicolons, commas, or whitespace. For this reason, you may want to use the core JavaScript global function encodeURIComponent( ) to encode the value before storing it in the cookie. If you do this, you'll have to use the corresponding decodeURIComponent( ) function when you read the cookie value. (It is also common to see code that uses the older escape( ) and unescape( ) functions, but these are now deprecated.)

A cookie written with a simple name/value pair lasts for the current web-browsing session but is lost when the user exits the browser. To create a cookie that can last across browser sessions, specify its lifetime (in seconds) with a max-age attribute. You can do this by setting the cookie property to a string of the form:

name=value; max-age=seconds



For example, to create a cookie that persists for a year, you can use code like this:

document.cookie = "version=" + document.lastModified +
                  "; max-age=" + (60*60*24*365);



You can also specify the lifetime of a cookie with the obsolete expires attribute, which should be set to a date in the format written by Date.toGMTString( ). For example:

var nextyear = new Date( );
nextyear.setFullYear(nextyear.getFullYear( ) + 1);
document.cookie = "version=" + document.lastModified +
                  "; expires=" + nextyear.toGMTString( );



Similarly, you can set the path, domain, and secure attributes of a cookie by appending strings of the following format to the cookie value before that value is written to the cookie property:

; path=path
; domain=domain
; secure



To change the value of a cookie, set its value again using the same name, path, and domain along with the new value. You can change the lifetime of a cookie when you change its value by specifying a new max-age or expires attribute.

To delete a cookie, set it again using the same name, path, and domain, specifying an arbitrary (or empty) value, and a max-age attribute of 0 (or use the expires attribute to specify an expiration date that has already passed). Note that the browser is not required to delete expired cookies immediately, so a cookie may remain in the browser's cookie file past its expiration date.

19.2.1. Cookie Limitations
Cookies are intended for infrequent storage of small amounts of data. They are not intended as a general-purpose communication or data-transfer mechanism, so you should use them in moderation. RFC 2965 encourages browser manufacturers to allow unlimited numbers of cookies of unrestricted size. You should know, however, that the standard does not require browsers to retain more than 300 cookies total, 20 cookies per web server (for the entire server, not just for your page or site on the server), or 4 KB of data per cookie (both name and value count toward this 4 KB limit). In practice, modern browsers allow many more than 300 cookies total, but the 4 KB size limit is still enforced by some.

19.3. Reading Cookies
When you use the cookie property in a JavaScript expression, the value it returns is a string that contains all the cookies that apply to the current document. The string is a list of name=value pairs separated by semicolons, where name is the name of a cookie, and value is its string value. This value does not include any of the attributes that may have been set for the cookie. To determine the value of a particular named cookie, you can use the String.indexOf( ) and String.substring( ) methods, or you can use String.split( ) to break the string into individual cookies.

Once you have extracted the value of a cookie from the cookie property, you must interpret that value based on whatever format or encoding was used by the cookie's creator. For example, the cookie might store multiple pieces of information in colon-separated fields. In this case, you would have to use appropriate string methods to extract the various fields of information. Don't forget to use the decodeURIComponent( ) function on the cookie value if it was encoded using the encodeURIComponent( ) function.

The following code shows how to read the cookie property, extract a single cookie from it, and use the value of that cookie:

// Read the cookie property. This returns all cookies for this document.
var allcookies = document.cookie;
// Look for the start of the cookie named "version"
var pos = allcookies.indexOf("version=");

// If we find a cookie by that name, extract and use its value
if (pos != -1) {
    var start = pos + 8;                       // Start of cookie value
    var end = allcookies.indexOf(";", start);  // End of cookie value
    if (end == -1) end = allcookies.length;
    var value = allcookies.substring(start, end);  // Extract the value
    value = decodeURIComponent(value);             // Decode it

    // Now that we have the cookie
 value, we can use it.
    // In this case, the cookie was previously set to the modification
    // date of the document, so we can use it to see if the document has
    // changed since the user last visited.
    if (value != document.lastModified)
        alert("This document has changed since you were last here");
}



Note that the string returned when you read the value of the cookie property does not contain any information about the various cookie attributes. The cookie property allows you to set those attributes, but it does not allow you to read them.

个人ajax技术专题站: " target="_blank">http://www. 我不会闲你烦,只会闲你不够烦!
2008-04-15 09:12
kevintang
Rank: 4
等 级:业余侠客
威 望:9
帖 子:799
专家分:236
注 册:2008-2-14
收藏
得分:0 
js 操作cookie
JS 操作COOKIE
<html>
<head>
<script>
//写cookies
function setCookie(name,value)
{
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//读取cookies
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg)) return unescape(arr[2]);
else return null;
}
//删除cookies
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
function ShowCookie()
{
alert(getCookie("name"));
}
function SaveCookie()
{
setCookie("name","hayden");
}
function setCookie(name,value,time){
var strsec = getsec(time);
var exp = new Date();
exp.setTime(exp.getTime() + strsec*1);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getsec(str){
   
    var str1=str.substring(1,str.length)*1;
    var str2=str.substring(0,1);
    if (str2=="s"){
    return str1*1000;
    }else if (str2=="h"){
    return str1*60*60*1000;
    }else if (str2=="d"){
    return str1*24*60*60*1000;
    }
}
</script>
</head>
<body>
<input type="button"  value="取" onclick="ShowCookie();" />
<input type="button"  value="存" onclick="SaveCookie();" /></body>
</html>
2008-04-16 16:02
快速回复:cookie的读写
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013992 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved