JavaScript Cookie对象

稿件来源: 太阳光网页设计   撰稿作者: 太阳光   发表日期: 2009-05-25 9:37:37   阅读次数: 313   查看权限: 游客查看

是一种以文件(Cookie文件)的形式保存在客户端硬盘的Cookies文件夹中的用户数据信息(Cookie数据)。Cookie文件由所访问的 Web站点建立,以长久的保存客户端与Web站点间的会话数据,并且该Cookie数据只允许被所访问的Web站点进行读取。

JavaScript Cookie对象
 
  • Cookie对象:

是一种以文件(Cookie文件)的形式保存在客户端硬盘的Cookies文件夹中的用户数据信息(Cookie数据)。Cookie文件由所访问的 Web站点建立,以长久的保存客户端与Web站点间的会话数据,并且该Cookie数据只允许被所访问的Web站点进行读取。

  • Cookie文件的格式:

NS:Cookie.txt
IE:用户名@域名.txt
 

  • 写入Cookie:

格式:

document.cookie = " 关键字 = 值 [ ; expires = 有效日期 ] [;...]"

备注:

  1. 有效日期格式:Wdy,DD-Mon-YY HH:MM:SS GMT
  2. Wdy / Mon:英文星期 / 月份;
  3. 还包含path、domain、secure属性;
  4. 每个Web站点(domain)可建立20个Cookie数据;
  5. 每个浏览器可存储300个Cookie数据,4k字节;
  6. 客户有权禁止Cookie数据的写入。
     

例1:

<Script>

var today = new Date();
var expireDay = new Date();
var msPerMonth = 24*60*60*1000*31;
expireDay.setTime( today.getTime() + msPerMonth );

document.cookie = "name=Hubert;expires=" + expireDay.toGMTString();
document.write("已经将 Cookie 写入你的硬盘中了!<br>");
document.write("内容是:", document.cookie, "<br>");
document.write("这个 Cookie 的有效时间是:");
document.write(expireDay.toGMTString());

</Script>

例2:

<Script>

var today = new Date();
var expireDay = new Date();
var msPerMonth = 24*60*60*1000*31;
expireDay.setTime( today.getTime() + msPerMonth );

function setCookie(Key,value) {
document.cookie = Key + "=" + value + ";expires=" + expireDay.toGMTString();
}

setCookie("NAME","HUBERT");
document.write("累计的 Cookies 如下:<BR>");
document.write(document.cookie);

</Script>

 

  • 读取Cookie:

格式:

document.cookie
 

<Script>

function getCookie(Key){
var search = Key + "=";
begin = document.cookie.indexOf(search);
if (begin != -1) {
    begin += search.length;
    end = document.cookie.indexOf(";",begin);
    if (end == -1) end = document.cookie.length;
    return document.cookie.substring(begin,end);
}
}

document.write("嗨! ",getCookie("name"), " 欢迎光临..")

</Script>

 

  • 删除Cookie:

格式:

document.cookie = " 关键字 = ; expires = 当前日期"
 

例:

<Script>

var today = new Date();

function delCookie(Key) {
document.cookie = Key + "=;expires=today.toGMTString";
}

document.write("现有的 Cookies 如下:<BR>");
document.write(document.cookie, "<BR>");
delCookie("name");
document.write("删除后的 Cookies 如下:<BR>");
document.write(document.cookie);

</Script>

关键词: Cookie对象,javascript,cookie   编辑时间: 2010-03-12 10:23:59

  • 感到高兴

    1

    高兴
  • 感到支持

    0

    支持
  • 感到搞笑

    0

    搞笑
  • 感到不解

    0

    不解
  • 感到谎言

    0

    谎言
  • 感到枪稿

    0

    枪稿
  • 感到震惊

    0

    震惊
  • 感到无奈

    0

    无奈
  • 感到无聊

    0

    无聊
  • 感到反对

    0

    反对
  • 感到愤怒

    0

    愤怒
50%(1)
50%(1)
共有0 条评论 发言请遵守【相关规定

网友评论

会员头像
发 表同步腾讯微博    验证码:  点击更新请先登陆
  • 暂无评论
关闭模块文章图片 article Pictrue
  • 我的妈妈爸爸
  • 基于koa2+mysql+vue2.0+Element阳光内容管理系统
  • 代码覆盖率工具 Istanbul 入门教程
  • 全栈工程师的武器——MEAN
  • 9款超炫的 CSS3 复选框(Checkbox)
  • 微信开发在线翻译功能
  • CSS3那些不为人知的高级属性
  • 给easyui的datebox添加清空事件
  • flash写字效果
  • kendoUI系列教程之DropDownList下拉菜单
  • kendoUI系列教程之datetimepicker日期时间选择
  • kendoUI系列教程之datepicker日期选择
  • kendoUI系列教程之combobox下拉列表框
  • kendoUI系列教程之colorpicker
  • kendoUI系列教程之calendar日历表
  • kendoUI系列教程之autocomplete自动补齐