设为首页 加入收藏

TOP

cookie清除及其他操作
2019-09-17 18:58:58 】 浏览:23
Tags:cookie 清除 及其他 操作

java script是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的。

而cookie是运行在客户端的,所以可以用JS来设置cookie.

一:设置cookie

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();
}

二:获取cookie

function getCookie(name){
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg)){
        return unescape(arr[2]);
    }else{
        return null;
    }
}

三:删除cookie

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();
    } 
}

四: 清除所有cookie

        
       function clearAllCookie() {
var date=new Date(); date.setTime(date.getTime()-10000); var keys=document.cookie.match(/[^ =;]+(?=\=)/g); console.log("需要删除的cookie名字:"+keys); if (keys) { for (var i = keys.length; i--;) document.cookie=keys[i]+"=0; expire="+date.toGMTString()+"; path=/"; } }

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇js 加密混淆工具 下一篇extjs grid禁止表格头部使用鼠标..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目