jQuery & Cookies (get/set/delete & a plugin)
来源:互联网 发布:使用迅雷下载网络异常 编辑:程序博客网 时间:2024/05/21 09:20
From: http://jquery-howto.blogspot.com/2010/09/jquery-cookies-getsetdelete-plugin.html
In this post I would like to share javascript functions that will help you easily get, set, delete and basically manage your cookies. Also, link to jQuery Cookie plugin, it’s improved version with more functions and of course easy to read and short examples on how to use these functions.
I will try to keep this post short and will not explain what cookies are and how to eat them. There are plenty of articles covering it already.
Here are javascript functions by Peter-Paul Koch to getCookie()
, setCookie()
and deleteCookie()
:
function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function deleteCookie(name) {
setCookie(name,"",-1);
}
/*
Changed function names from readCookie(), createCookie()
and eraseCookie() to getCookie(), setCookie() and
deleteCookie().
*/
Here is an example that shows you how to use those functions in your javascript to create, edit and delete your cookies:
// Create/write a cookie and store it for 1 day
setCookie('myCookie', 'myValue', 1);
// Get my cookie
getCookie('myCookie');
// Delete/erase my cookie
deleteCookie('myCookie');
This 3 javascript function is all you need manage your cookies, but if you want to do it jQuery style than you can use jQuery Cookie plugin or it’s improved version.
Here is how to use jQuery Cookie plugin in your code:
// Setting a cookie
$.cookie('myCookie':'myValue');
// Creating cookie with all availabl options
$.cookie('myCookie2', 'myValue2', { expires: 7, path: '/', domain: 'example.com', secure: true });
// Get a cookie
$.cookie('myCookie');
// Delete a cookie
$.cookie('myCookie', null);
With an improved version of the plugin you can set and get multiple cookies in one call. The improved version of the jQuery Cookie pluin only adds few additional bytes.
// Set multiple cookies
$.cookie({ 'cookie1':'value1', 'cookie2':'value2' });
- jQuery & Cookies (get/set/delete & a plugin)
- A Cgi Set Cookies
- A Cgi Get Cookies
- JQuery详细教程三之HTML(get,add,set,delete方法)
- Tutorial: Creating a jQuery plugin
- Get/Set a bean property
- [HtmlParser]bug提交(含解决方案)--A bug when set cookies
- memcached--delete--replace--set--get--incr--decr--stats
- A jQuery Plugin for Zoomable, Interactive Maps
- How To Develop a jQuery Plugin
- A Really Simple jQuery Plugin Tutorial
- jquery中GET和SET的区别
- A. Cookies
- A. Cookies
- A. Cookies
- jquery.ajax post/get/delete/put 请求方法封装
- “'cookies.txt' does not look like a Set-Cookie3 (LWP) format file”和“invalid Netscape format cookies”
- Response.Cookies.Set() vs. Response.Cookies[] =
- SQL*Plus 学习笔记——常用编辑命令
- Latex 插入图片
- Static import
- 通过设置来提高Linux上socket性能
- 妥善处理解决网络I/O瓶颈
- jQuery & Cookies (get/set/delete & a plugin)
- 10 Defining points of Extreme Programming
- 手机RF开发——前言
- 手机RF开发——驱动开发
- emule(电驴/电骡)源码分析
- 手机RF开发——HW development
- 手机RF专题讲座
- PART II SYSTEMS PROGRAMMING
- 深入浅出Win32多线程程序设计(一)