jquery.cookie.js的插件,插件的源代码如下:

  1. /**
  2. * Cookie plugin
  3. *
  4. * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
  5. * Dual licensed under the MIT and GPL licenses:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. * http://www.gnu.org/licenses/gpl.html
  8. *
  9. */
  10.  
  11. /**
  12. * Create a cookie with the given name and value and other optional parameters.
  13. *
  14. * @example $.cookie('the_cookie', 'the_value');
  15. * @desc Set the value of a cookie.
  16. * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
  17. * @desc Create a cookie with all available options.
  18. * @example $.cookie('the_cookie', 'the_value');
  19. * @desc Create a session cookie.
  20. * @example $.cookie('the_cookie', null);
  21. * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
  22. * used when the cookie was set.
  23. *
  24. * @param String name The name of the cookie.
  25. * @param String value The value of the cookie.
  26. * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
  27. * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
  28. * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
  29. * If set to null or omitted, the cookie will be a session cookie and will not be retained
  30. * when the the browser exits.
  31. * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
  32. * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
  33. * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
  34. * require a secure protocol (like HTTPS).
  35. * @type undefined
  36. *
  37. * @name $.cookie
  38. * @cat Plugins/Cookie
  39. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  40. */
  41.  
  42. /**
  43. * Get the value of a cookie with the given name.
  44. *
  45. * @example $.cookie('the_cookie');
  46. * @desc Get the value of a cookie.
  47. *
  48. * @param String name The name of the cookie.
  49. * @return The value of the cookie.
  50. * @type String
  51. *
  52. * @name $.cookie
  53. * @cat Plugins/Cookie
  54. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  55. */
  56.  
  57. jQuery.cookie = function(name, value, options) {
  58. if (typeof value != 'undefined') { // name and value given, set cookie
  59. options = options || {};
  60. if (value === null) {
  61. value = '';
  62. options.expires = -;
  63. }
  64. var expires = '';
  65. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  66. var date;
  67. if (typeof options.expires == 'number') {
  68. date = new Date();
  69. date.setTime(date.getTime() + (options.expires * * * * ));
  70. } else {
  71. date = options.expires;
  72. }
  73. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  74. }
  75. // CAUTION: Needed to parenthesize options.path and options.domain
  76. // in the following expressions, otherwise they evaluate to undefined
  77. // in the packed version for some reason...
  78. var path = options.path ? '; path=' + (options.path) : '';
  79. var domain = options.domain ? '; domain=' + (options.domain) : '';
  80. var secure = options.secure ? '; secure' : '';
  81. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  82. } else { // only name given, get cookie
  83. var cookieValue = null;
  84. if (document.cookie && document.cookie != '') {
  85. var cookies = document.cookie.split(';');
  86. for (var i = ; i < cookies.length; i++) {
  87. var cookie = jQuery.trim(cookies[i]);
  88. // Does this cookie string begin with the name we want?
  89. if (cookie.substring(, name.length + ) == (name + '=')) {
  90. cookieValue = decodeURIComponent(cookie.substring(name.length + ));
  91. break;
  92. }
  93. }
  94. }
  95. return cookieValue;
  96. }
  97. };

Cookie插件的操作

创建一个会话cookie:

$.cookie(‘cookieName’,'cookieValue’);

注:当没有指明cookie时间时,所创建的cookie有效期默认到用户浏览器关闭止,故被称为会话cookie。

创建一个持久cookie:

$.cookie(‘cookieName’,'cookieValue’,{expires:7});

注:当指明时间时,故称为持久cookie,并且有效时间为天。

创建一个持久并带有效路径的cookie:

$.cookie(‘cookieName’,'cookieValue’,{expires:7,path:’/'});

注:如果不设置有效路径,在默认情况下,只能在cookie设置当前页面读取该cookie,cookie的路径用于设置能够读取cookie的顶级目录。

创建一个持久并带有效路径和域名的cookie:

$.cookie(‘cookieName’,'cookieValue’,{expires:7,path:’/',domain: ‘chuhoo.com’,secure: false,raw:false});

注:domain:创建cookie所在网页所拥有的域名;secure:默认是false,如果为true,cookie的传输协议需为https;raw:默认为false,读取和写入时候自动进行编码和解码(使用encodeURIComponent编码,使用decodeURIComponent解码),关闭这个功能,请设置为true。

获取cookie:

$.cookie(‘cookieName’);   //如果存在则返回cookieValue,否则返回null。

删除cookie:

$.cookie(‘cookieName’,null);

或者这样也能删除: $.cookie('cookieName', '', { expires: -1, path: '/' }); // 删除 cookie

注:如果想删除一个带有效路径的cookie,如下:$.cookie(‘cookieName’,null,{path:’/'});

来演示一个换肤的粟子:

代码之div+css

  1. <link rel="stylesheet" href="styles/skin/skin_0.css" type="text/css" id="cssfile" />
  2.  
  3. <!--头部开始-->
  4. <div id="header">
  5. <a id="logo" href="#">Jane Shopping</a>
  6. <ul id="skin">
  7. <li id="skin_0" title="蓝色" class="selected">蓝色</li>
  8. <li id="skin_1" title="紫色">紫色</li>
  9. <li id="skin_2" title="红色">红色</li>
  10. <li id="skin_3" title="天蓝色">天蓝色</li>
  11. <li id="skin_4" title="橙色">橙色</li>
  12. <li id="skin_5" title="淡绿色">淡绿色</li>
  13. </ul>
  14.  
  15. </div>
  16. <!--头部结束-->
  1. /*切换皮肤样式*/
  2. #skin {
  3. float:right;
  4. margin:10px;
  5. padding:4px;
  6. width:120px;
  7. list-style:none;
  8. border: 1px solid #CCCCCC;
  9. background:#FFF;
  10. }
  11. #skin li {
  12. float:left;
  13. margin-right:4px;
  14. width:15px;
  15. height:15px;
  16. text-indent:-9999px;
  17. overflow:hidden;
  18. display:block;
  19. cursor:pointer;
  20. background-image:url(../images/theme.gif);
  21. }
  22. #skin_0 { background-position:0px 0px; }
  23. #skin_1 { background-position:15px 0px; }
  24. #skin_2 { background-position:35px 0px; }
  25. #skin_3 { background-position:55px 0px; }
  26. #skin_4 { background-position:75px 0px; }
  27. #skin_5 { background-position:95px 0px; }
  28. #skin_0.selected { background-position:0px 15px; }
  29. #skin_1.selected { background-position:15px 15px; }
  30. #skin_2.selected { background-position:35px 15px; }
  31. #skin_3.selected { background-position:55px 15px; }
  32. #skin_4.selected { background-position:75px 15px; }
  33. #skin_5.selected { background-position:95px 15px; }

不同皮肤的css文件都放在styles/skins 文件夹下,命名如下:

代码之jQuery代码

版本一:

  1. $(function(){
  2. var $li =$("#skin li");
  3. $li.click(function(){
  4. $("#"+this.id).addClass("selected") //当前<li>元素选中
  5. .siblings().removeClass("selected"); //去掉其它同辈<li>元素的选中
  6. $("#cssfile").attr("href","styles/skin/"+ (this.id) +".css"); //设置不同皮肤
  7. $.cookie( "MyCssSkin" , this.id , { path: '/', expires: });
  8. });
  9. var cookie_skin = $.cookie( "MyCssSkin");//将MyCssSkin这个cookie值this.id赋给变量cookie_skin
  10.         if (cookie_skin) {
  11. $("#"+cookie_skin).addClass("selected") //当前<li>元素选中
  12. .siblings().removeClass("selected"); //去掉其它同辈<li>元素的选中
  13. $("#cssfile").attr("href","styles/skin/"+ cookie_skin +".css"); //设置不同皮肤
  14. $.cookie( "MyCssSkin" , cookie_skin , { path: '/', expires: });
  15. }
  16. })

版本一不好的地方,就是有大量的重复代码,可以模块化,函数化,来看版本二:

  1. //网站换肤
  2. $(function(){
  3. var $li =$("#skin li");
  4. $li.click(function(){
  5. switchSkin( this.id );
  6. });
  7. var cookie_skin = $.cookie("MyCssSkin");
  8. if (cookie_skin) {
  9. switchSkin( cookie_skin );
  10. }
  11. });
  12.  
  13. function switchSkin(skinName){
  14. $("#"+skinName).addClass("selected") //当前<li>元素选中
  15. .siblings().removeClass("selected"); //去掉其他同辈<li>元素的选中
  16. $("#cssfile").attr("href","styles/skin/"+ skinName +".css"); //设置不同皮肤
  17. $.cookie( "MyCssSkin" , skinName , { path: '/', expires: });
  18. }

下面代码也是换肤的例子

html:

  1. <script>
  2. //加载皮肤文件
  3. loadCss(_skinId,'Main.css');
  4. </script>
  5.  
  6. <div style="right:10px; top: 5px; position: absolute; height: 21px; text-align:right">
  7. <a>皮肤:</a><a href="#" onclick="loadSkin('1')">藤蔓绿</a>&nbsp;<a href="#" onclick="loadSkin('2')">经典蓝</a>&nbsp;<a href="#" onclick="loadSkin('3')">甜蜜橙</a>&nbsp;<a href="#" onclick="loadSkin('4')">淡雅灰</a>
  8. </div>

皮肤路径 :

js代码:

  1. var _skinId = getCookie("_skinId") ? getCookie("_skinId"):"";
  2.  
  3. function loadSkin(skinId)
  4. {
  5. writeCookie("_skinId",skinId);
  6. top.window.location.reload();
  7. }
  8.  
  9. function $(id)
  10. {
  11. return document.getElementById(id);
  12. }
  13.  
  14. function loadCss(skinId,cssName)
  15. {
  16. var head = document.getElementsByTagName('head').item();
  17. css = document.createElement('link');
  18. css.href = "Skin/Skin" + skinId + "/"+cssName;
  19. css.rel = 'stylesheet';
  20. css.type = 'text/css';
  21. css.id = 'loadCss';
  22. head.appendChild(css);
  23. }
  24.  
  25. function loadJs(file)
  26. {
  27. var scriptTag = document.getElementById('loadScript');
  28. var head = document.getElementsByTagName('head').item();
  29. if(scriptTag) head.removeChild(scriptTag);
  30. script = document.createElement('script');
  31. script.src = "../js/mi_"+file+".js";
  32. script.type = 'text/javascript';
  33. script.id = 'loadScript';
  34. head.appendChild(script);
  35. }
  36.  
  37. function setCookie(sKey, sValue, sDomain, sPath, sExpires, blSecure)
  38. {
  39. var sCookieStr = sKey + "=" + sValue + ";";
  40.  
  41. if (sDomain) sCookieStr += " DOMAIN=" + sDomain + ";";
  42. if (sPath) sCookieStr += " PATH=" + sPath + ";";
  43. if (sExpires) sCookieStr += " EXPIRES=" + sExpires + ";";
  44. if (blSecure) sCookieStr += " SECURE";
  45.  
  46. document.cookie = sCookieStr;
  47. }
  48.  
  49. function writeCookie(key,value)
  50. {
  51. var sExpires=new Date();
  52. sExpires.setTime(sExpires.getTime()+****);
  53. setCookie(key,value,"","",sExpires.toGMTString(),"");
  54. if(getCookie(key) == null)
  55. {
  56. alert("您的浏览器安全设置过高,不支持Cookie,请重新设置浏览器的。");
  57. }
  58. }
  59.  
  60. function getCookie(sKey)
  61. {
  62. var sCookie = document.cookie;
  63. if( !sCookie ) return null;
  64.  
  65. var sTag = sKey + "=";
  66.  
  67. var iBegin = sCookie.indexOf(sTag);
  68. if (iBegin < ) return null;
  69.  
  70. iBegin += sTag.length;
  71.  
  72. var iEnd = sCookie.indexOf(";", iBegin);
  73. if (iEnd < ) iEnd = sCookie.length;
  74.  
  75. return sCookie.substring(iBegin, iEnd);
  76. }
  77.  
  78. function delCookie(sKey)
  79. {
  80. var tNow = new Date();
  81. setCookie(sKey, "", null, null, tNow.toGMTString(), null);
  82. }

jquery.cookie中的操作之与换肤的更多相关文章

  1. jquery.cookie中的操作

    http://w3school.com.cn/js/js_cookies.asp jquery.cookie中的操作: jquery.cookie.js是一个基于jquery的插件,点击下载! 创建一 ...

  2. (转)jquery.cookie中的操作

      jquery.cookie中的操作: jquery.cookie.js是一个基于jquery的插件,点击下载! 创建一个会话cookie: $.cookie(‘cookieName’,'cooki ...

  3. 5.2 《锋利的jQuery》jQuery对表格的操作(选项卡/换肤)

    表格隔行变色以及单选/复选 表格展开关闭 表格筛选 字体变大/缩小 选项卡 网页换肤 tip1: $("tr:odd")和$("tr:even")选择器索引是从 ...

  4. vue中利用scss实现整体换肤和字体大小设置

    一.前言 利用Sass预处理实现换肤和字体大小调整. 思路及达到的效果:字体大小的适配使用window.devicePixelRatio的值和需要调整的差量进行控制.页面初始化是的字体适配可以根据de ...

  5. Android中插件开发篇之----应用换肤原理解析

    一.前言 今天又到周末了,感觉时间过的很快呀.又要写blog了.那么今天就来看看应用的换肤原理解析.在之前的一篇博客中我说道了Android中的插件开发篇的基础:类加载器的相关知识.没看过的同学可以转 ...

  6. vue 中使用sass实现主体换肤

    有如下代码要实现换肤功能 <template> <div class="app-root" :class="themeClass"> & ...

  7. jQuery + Cookie引导客户操作

    网址:http://www.sucaihuo.com/js/707.html 示例:http://www.sucaihuo.com/jquery/7/707/demo/

  8. jquery.cookie 使用文档,$.cookie() 文档教程, js 操作 cookie 教程文档。

    jquery.cookie 使用文档,$.cookie() 文档教程, js 操作 cookie 教程文档. jquery.cookie中的操作: jquery.cookie.js是一个基于jquer ...

  9. 管理Cookie的插件——jquery.cookie.js

    下载地址:http://plugins.jquery.com/cookie/ jquery.cookie中的操作: 一.创建cookie: 1.创建一个会话cookie: $.cookie('cook ...

随机推荐

  1. 解决eclipse中java代码注释变成乱码的问题

    Eclipse JAVA文件注释乱码将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,主要原因就是别人的IDE编码格式和自己的Eclipse编码 ...

  2. nio selector

    为什么使用Selector? 仅用单个线程来处理多个Channels的好处是,只需要更少的线程来处理通道.事实上,可以只用一个线程处理所有的通道.对于操作系统来说,线程之间上下文切换的开销很大,而且每 ...

  3. 【mybatis】mybatis中批量插入 批量更新 batch 进行insert 和 update,或者切割LIst进行批量操作

    ================================================================== 分别展示 mybatis 批量新增  和 批量更新   的操作: ...

  4. 通用Json的处理办法

    1.Json的格式: 对象{"name": "value", "name1": "value1"} 对象包含对象数组{& ...

  5. Tomcat 访问 Manager App,Host Manager

     1.启动tomcat,在浏览器输入:http://localhost:8080/ 2.配置tomcat-users.xml 文件 在主目录的cong文件夹下找到tomcat-users.xml 文件 ...

  6. [Python爬虫] Selenium +phantomjs 模拟下拉滚动条

    在爬虫中,有时会遇到这种情况,数据的展示是不是一页一页的,而是通过不断的下拉滚动条来加载数据.例如一点咨询(http://www.yidianzixun.com/)和微博(在未登录的状态下:http: ...

  7. 使用CALayer实现图像镜面效果

    在iOS中,可以使用QuartzCore.framework基于CALayer做一些图像效果,不清楚CALayer,请先看这篇. 在这里我们给图像做一个简单的镜面反射效果,要学习一些图像变化的知识,首 ...

  8. Media-媒介(媒体、介质)【译】

    Media-媒介(媒体.介质) 转载请注明来源:http://blog.csdn.net/lifeshow           Android支持定制的媒介解码器,需要将定制的解码器接口暴露给框架. ...

  9. C# 获取父控件容器的属性

    C# 获取父控件容器的属性 BindingNavigator bindingNavigator = (sender as ToolStripButton).GetCurrentParent() as ...

  10. c语言指针详解 经典

    指针是C语言中广泛使用的一种数据类型. 运用指针编程是C语言最主要的风格之一.利用指针变量可以表示各种数据结构: 能很方便地使用数组和字符串: 并能象汇编语言一样处理内存地址,从而编出精练而高效的程序 ...