cookie与封装

<!DOCTYPE HTML>
<html lang="zh">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="applicable-device" content="pc,mobile"/>
    <meta name="renderer" content="webkit"/>
    <meta name="keywords" content=""/>
    <meta name="description" content=""/>
    <title>cookie与封装</title>
</head>
<body>

<script src="http://s0.ifengimg.com/static/js/jquery-1.7.2.min_c4de8813.js"></script>
<script>
var cookieUtil={
    set:function(name,value,expires,path,domain,secure){//设置cookie
        var cookieName=encodeURIComponent(name)+‘=‘+encodeURIComponent(value);
        if(expires instanceof Date){
            cookieName+= ‘;expires=‘+expires;
        }
        if(path){
            cookieName+= ‘;path=‘+path;
        }
        if(domain){
            cookieName+= ‘;domain=‘+domain;
        }
        if(secure){
            cookieName+= ‘;secure‘;
        }
        document.cookie=cookieName;

    },
    setDate:function(day){//设置过期时间
        var date=null;
        if(typeof day ==‘number‘ && day>0){
            date=new Date();
            date.setDate(date.getDate()+day);
        }else{
            throw new Error(‘你输入的天数不合法,请输入大于0的数字‘);
        }
        return date;

    },
    get:function(name){//获取cookie
        var cookieName=encodeURIComponent(name)+‘=‘,
                cookieStart,
                cookieValue;
        cookieStart=document.cookie.indexOf(cookieName);
        if(cookieStart>-1){
            var cookieEnd=document.cookie.indexOf(‘;‘,cookieStart);
            if(cookieEnd == -1){
                cookieEnd=document.cookie.length;
            }
            cookieValue=decodeURIComponent(document.cookie.substring(cookieStart+cookieName.length,cookieEnd));
        }
        return cookieValue;

    },
    unSet:function(name,path,domain,secure){//删除cookie
        cookieUtil.set(name,‘‘,new Date(0),path,domain,secure);

    }
};

//测试设置cookie
/*cookieUtil.set(‘use‘,‘xiangcy‘,cookieUtil.setDate(1));
cookieUtil.set(‘url‘,‘ycvo.com‘,cookieUtil.setDate(28));
cookieUtil.set(‘email‘,‘[email protected]‘);*/

//测试删除cookie
/*
cookieUtil.unSet(‘use‘);
cookieUtil.unSet(‘url‘);
cookieUtil.unSet(‘email‘);
*/

//测试获取cookie
alert(cookieUtil.get(‘use‘));
alert(cookieUtil.get(‘url‘));
alert(cookieUtil.get(‘email‘));

</script>
</body>
</html>

  

时间: 2024-10-17 06:50:20

cookie与封装的相关文章

javascript应用cookie的封装代码

javascript应用cookie的封装代码: 关于cookie的优缺点这里就不多介绍了,都是老生长谈了,不过对于cookie的操作可能很多学习者比较陌生,下面分享一个封装好的cookie操作代码,希望能够给需要的朋友带来一定的帮助. 代码如下: var cookie=new function(){ this.set=function(name,value,hours){ var life=new Date().getTime(); life+=hours*1000*60; var cooki

.Net mvc cookie 操作封装方法

对cookie操作不太熟悉的可以参考一下. 一般来说 cookie是个针对访问者电脑存储数据的东西,可以把一个cookie看作一个表,有表名,可以有很多行,每一行有两个字段,key和value,key唯一,可以通过key找到对应的值 value. 封装方法: #region 设置cookie /// <summary> /// 设置cookie /// </summary> /// <param name="key">键</param>

简单的cookie读写封装

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>简单的cookie封装</title></head><body> </body><script>var cookies={ name:this.name, value:this.value, day:this.da

cookie的封装和删除修改

一.设置 name = value; path = 路径; expires = new Date(); var d = new Date(); d.setDate(30) document.cookie = "password = tp;path = /;expires = "+d var d2 = new Date(); d2.setDate(30) document.cookie = "password = 123456;path = /; expires = "

cookie的封装

今天逛论坛,看到一个看起来写得好的函数,特此贴出分享: 原文地址[http://www.html-js.com/article/2638 ] 这个地址[https://github.com/jaywcjlove/cookie.js]中包含的是相关的方法和使用说明.

设置cookie、获取cookie、删除cookie函数封装

//设置cookie函数function setCookie(key,value,t){   var oDate = new Date();   oDate.setDate( oDate.getDate() + t );   document.cookie = key + '=' + encodeURI(value) + ';expires=' + oDate.toGMTString();} //获取cookie函数function getCookie(key){ //通过分号空格拆分开所有co

cookie方法封装及cookie缺点分析

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Consolas; color: #4f5d66 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Consolas; color: #b58a00 } span.s1 { color: #4663cc } span.s2 { color: #698906 } span.s3 { color: #b58a00 } span.s4 {

js cookie的封装和调用

<script> function setCookie(cname,cvalue,exdays){ var d = new Date(); d.setTime(d.getTime()+(exdays*24*60*60*1000)); var expires = "expires="+d.toGMTString(); document.cookie = cname+"="+cvalue+"; "+expires; } function

js操作cookie简单封装

关于cookie就不做多解释了,直接上代码吧,包括:设置cookie.删除cookie.获取cookie 随便带个说明 : * HTTP Cookie:存储会话信息 * 名称和值传送时必须是经过RUL编码的 * cookie绑定在指定的域名下,非本域无法共享cookie,但是可以是在主站共享cookie给子站 * cookie有一些限制:比如IE6 & IE6- 限定在20个:IE7 50个:Opear 30个...所以一般会根据[必须]需求才设定cookie * cookie的名称不分大小写: