JS - Cookie: getCookie, setCookie

JS function for Cookie

如果cookie未设置,判断时与‘’比较;

1 function setCookie(cname, cvalue, exdays) {
2     var d = new Date();
3     d.setTime(d.getTime() + (exdays*24*60*60*1000));
4     var expires = "expires="+d.toUTCString();
5     document.cookie = cname + "=" + cvalue + "; " + expires;
6 }
7     
 1 function getCookie(cname) {
 2     var name = cname + "=";
 3     var ca = document.cookie.split(‘;‘);
 4     for(var i=0; i<ca.length; i++) {
 5         var c = ca[i];
 6         while (c.charAt(0)==‘ ‘) c = c.substring(1);
 7         if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
 8     }
 9     return "";
10 }

当使用jQuery cookie时,

$.cookie(‘the_cookie‘); // 读取 cookie
$.cookie(‘the_cookie‘, ‘the_value‘); // 存储 cookie
$.cookie(‘the_cookie‘, ‘the_value‘, { expires: 7 }); // 存储一个带7天期限的 cookie
$.cookie(‘the_cookie‘, ‘‘, { expires: -1 }); // 删除 cookie

如果cookie未设置,判断时与undefined比较;

时间: 2024-10-22 15:46:30

JS - Cookie: getCookie, setCookie的相关文章

JS cookie的使用

js设置cookie有很多种方法. 第一种:(这个是w3c官网的代码) <script> //设置cookie function setCookie(cname, cvalue, exdays,cpath) {//注:JS中的函数调用可以和定义时参数个数不同,调用时如不需要可以不加后两个参数或最后一个参数) var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = "expir

js cookie

1. 设置COOKIE 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; } 函数解

Js COOkie 读取

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head>    <meta http-equiv="Content-Type" content="text/html; charset=GB2312" />    <title&g

js 操作cookie,getCookie,setCookie ,removeCookie

document.cookie = 'name='+'username'; document.cookie = 'value='+'value1'; var cookarr = document.cookie.replace(/\s/g,"").split(';');//将cookie的空格全部替换掉,不然会出现空格,使结果无法判断 var havcome = false; for (var i=0;i<cookarr.length;i++){ var str = cookarr

设置获取cookie,setCookie,getCookie

设置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 getCo

JS cookie 设置 查看 删除

JScookie 常用的3个预设函数(库) <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> function setCookie(name, value, iDay) //iday是多少天后过期 { var oDate=new Date(); oDate.setDate(oDate.ge

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

[snippet] js cookie operate

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 opCookie:function(){                                  var setCookie= function (name,value)                 {   var Days = 30;                         var exp = new Date();                       

【原】js cookie 数组 存读

自己研究了一下. "Cookie里面只能放String 类型" 所以只能将arr的数据按照自己的约定转成string格式存进cookie. 这里提示一下cookie是存在本地浏览器,session是服务器上的. 将下列的数组存进cookie. 1 var arr = [ 2 {'name':'joe'}, 3 {'name':'json'}, 4 {'name':'boe'}, 5 ]; 首先是存的function function setCookie(searchWord){ var