js jquery 设置cookie
转自http://yaoqianglilan.blog.163.com/blog/static/70978316201091810435251/
本人亲测setcookie() getcookie()非常好用,其它待测试
1.js设置cookie
//set 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();
}
function getcookie(name){
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null){
return unescape(arr[2]);
}else{
return "";
}
}
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();
}
//delcookie("GD-cms");
if (document.cookie != "") {
setcookie("GD-cms", "gae-django-cms");
}
alert(getcookie("GD-cms"));
2.jquery设置cookie 是封装到一个方法里面的 直接调用 跟php 差不多 取 设置 或者 删除
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// NOTE Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' (options.path) : '';
var domain = options.domain ? '; domain=' (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i ) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length 1) == (name '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length 1));
break;
}
}
}
return cookieValue;
}
};
使用方法
jQuery操作cookie的插件,大概的使用方法如下
$.cookie('the_cookie'); //读取Cookie值
$.cookie('the_cookie', 'the_value'); //设置cookie的值
$.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});//新建一个cookie 包括有效期 路径 域名等
$.cookie('the_cookie', 'the_value'); //新建cookie
$.cookie('the_cookie', null); //删除一个cookie
设置一个名称为blog,值为css9.net的cookie:
$.cookie("blog", "css9.net");
设置一个名称为blog,值为css9.net的cookie,同时设置过期时间(expires属性)为7天:
$.cookie("blog", "css9.net", { expires: 7 });
设置一个名称为blog,值为css9.net的cookie,设置过期时间(expires属性)为7天,同时设置cookie的path属性为”/admin”
$.cookie("blog", "css9.net", { path: '/admin', expires: 7 });
读取Cookie:
读取名称为blog的cookie值:
alert( $.cookie("blog") );
删除cookie:
$.cookie("example", null);
如果你使用了jquery插件 建议 用jquery自带的 如果 没有 就不必为了设置cookie 引入jquery了 毕竟 插件加载还是比较大的
js jquery 设置cookie的更多相关文章
- Jquery设置Cookie
jQuery代码: <script src="js/jquery-1.3.1.js" type="text/javascript"></scr ...
- 通过js来设置cookie和读取cookie,实现登陆时记住密码的功能
function setCookie(){ //设置cookie var loginCode = $("#login_code").val(); //获取用户名信息 var pwd ...
- jquery 设置cookie、删除cookie、获取cookie
1.引入jquery.js <script src="//cdn.bootcss.com/jquery/1.12.4/jquery.js"></script> ...
- 简易的JQuery设置Cookie
使用之前先引用这两个文件: 然后基本的功能代码如下: <div> <input id="txtDelValues" type="text" / ...
- JS中设置cookie,读取cookie,删除cookie
在开发时,碰到一个需求,需要保存一个表的信息(非隐私),希望下次打开还存在.于是想到用cookie,一番折腾完成.示例数据都是假的,打马赛克是怕泄密. 这个表取名为Data,为Array,每一行是一个 ...
- JS/JQuery 设置input等标签设置和取消只读属性
<input type="text" id="HouseName" value="" align="left"/& ...
- jquery.cookie.js——jquery的cookie插件
一.JS文件 /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Copyright 2 ...
- <<< 网页中如何利用原生js和jquery储存cookie
javascript当中的cookie机制,使应用达到了真正的全局变量的要求,cookie是浏览器提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由Java ...
- js设置cookie(原生js)
cookie 与 session 是网页开发中常用的信息存储方式.Cookie是在客户端开辟的一块可存储用户信息的地方:Session是在服务器内存中开辟的一块存储用户信息的地方. JavaScrip ...
随机推荐
- learning scala 变量
scala 变量: val : 声明时,必须被初始化,不能再重新赋值. scala> test = "only1"<console>:11: error: not ...
- Graph
题目: [问题描述] 给你一个有向图,有 N 个点,标号为 0 到 N -1,图中的每条边有个权值,每次你经过一条边,它的权值将被记入你的得分,如果同样的边被经过多次,它的权值每次都将被记入总分,权值 ...
- 实现checkbox的多选
checkbox多选 技术一般水平有限,有什么错的地方,望大家指正. 全选,多选都是为了使用的方便,一般情况下全选就够用了,但是用户要求实现一个多选的功能也没有办法老老实实的做吧. 多选的实现也较为简 ...
- 去除字符串中的emoji字符
对于使用utf8编码的mysql数据库来说,如果字符串中存在emoji小图像,是不能存进数据库中的,查了一下,原因大概是因为utf8编码可以存1-3个字节的字符,但是emoji是4个字节:解决方法可以 ...
- HTTPS工作原理和TCP握手机制
1.HTTPS的工作原理 HTTPS在传输数据之前需要客户端(浏览器)与服务端(网站)之间进行一次握手, 在握手过程中将确立双方加密传输数据的密码信息. TLS/SSL协议不仅仅是一套加密传输的协议, ...
- anu - react
import { options } from "./util"; import { Children } from "./Children"; import ...
- AR.Drone 2.0四轴飞机体验:最好的玩具航拍器
http://digi.tech.qq.com/a/20140513/007458.htm?pgv_ref=aio2012&ptlang=2052 AR.Drone 2.0四轴飞机体验:最好的 ...
- 【c++基础】如何获取工程项目当前路径
工程项目当前路径 #include <direct.h> int main( ) { ]; _getcwd(buffer, ); std::cout << buffer < ...
- 你在AutoHotKey面前居然敢比调音量 - imsoft.cnblogs
当你正在电脑游戏中酣战之际.或者正沉浸在动作大片紧张激烈的情节中.或者正在全神贯注的聆听优美动听音乐……,在这些场景中,如果你需要迅速对音量进行调节(例如增大减小音量,或者静音)怎么办?难道返回Win ...
- CH4101 银河英雄传说
题意 4101 银河英雄传说 0x40「数据结构进阶」例题 描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七 ...