Javascript 剥离Jquery Cookie脚本
我正在努力减少我网站上的javascript开销,我正在使用这个插件: 代码如下:Javascript 剥离Jquery Cookie脚本,javascript,jquery,cookies,Javascript,Jquery,Cookies,我正在努力减少我网站上的javascript开销,我正在使用这个插件: 代码如下: /*! * jQuery Cookie Plugin v1.3 * https://github.com/carhartl/jquery-cookie * * Copyright 2011, Klaus Hartl * Dual licensed under the MIT or GPL Version 2 licenses. * http://www.opensource.org/licenses
/*!
* jQuery Cookie Plugin v1.3
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2011, Klaus Hartl
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
(function ($, document, undefined) {
var pluses = /\+/g;
function raw(s) {
return s;
}
function decoded(s) {
return decodeURIComponent(s.replace(pluses, ' '));
}
var config = $.cookie = function (key, value, options) {
// write
if (value !== undefined) {
options = $.extend({}, config.defaults, options);
if (value === null) {
options.expires = -1;
}
if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
value = config.json ? JSON.stringify(value) : String(value);
return (document.cookie = [
encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
// read
var decode = config.raw ? raw : decoded;
var cookies = document.cookie.split('; ');
for (var i = 0, l = cookies.length; i < l; i++) {
var parts = cookies[i].split('=');
if (decode(parts.shift()) === key) {
var cookie = decode(parts.join('='));
return config.json ? JSON.parse(cookie) : cookie;
}
}
return null;
};
config.defaults = {};
$.removeCookie = function (key, options) {
if ($.cookie(key) !== null) {
$.cookie(key, null, options);
return true;
}
return false;
};
})(jQuery, document);
/*!
*jQueryCookie插件v1.3
* https://github.com/carhartl/jquery-cookie
*
*版权所有2011,Klaus Hartl
*MIT或GPL版本2许可下的双重许可。
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
(函数($,文档,未定义){
var pluses=/\+/g;
函数原始值{
返回s;
}
已解码功能{
返回组件(替换(加上“,”);
}
var config=$.cookie=函数(键、值、选项){
//写
如果(值!==未定义){
options=$.extend({},config.defaults,options);
如果(值===null){
options.expires=-1;
}
if(typeof options.expires==='number'){
var days=options.expires,t=options.expires=new Date();
t、 设置日期(t.getDate()+天);
}
value=config.json?json.stringify(值):字符串(值);
返回(document.cookie=[
encodeURIComponent(键),“=”,config.raw?值:encodeURIComponent(值),
options.expires?';expires='+options.expires.toutString():'',//使用expires属性,IE不支持最大年龄
options.path?';path='+options.path:“”,
options.domain?';domain='+options.domain:'',
options.secure?“;secure”:”
].加入(“”);
}
//阅读
var decode=config.raw?raw:已解码;
var cookies=document.cookie.split(“;”);
对于(变量i=0,l=cookies.length;i
这是我通过删除“写”代码来剥离它的尝试
(函数($,文档,未定义){
var pluses=/\+/g;
函数原始值{
返回s;
}
已解码功能{
返回组件(替换(加上“,”);
}
var config=$.cookie=函数(键、值、选项){
//阅读
var decode=config.raw?raw:已解码;
var cookies=document.cookie.split(“;”);
对于(变量i=0,l=cookies.length;i
如果我只想用它来阅读cookies,而不是删除、写入和读取cookies,那么我还有没有多余的东西可以去掉呢。好吧,用jQuery插件来阅读cookies有点傻,尽管我用同一个插件做了同样的事情 有一些简短的代码,您可以使用
所有这些的核心都是document.cookie——它们仅仅是一个字符串,而不是一个漂亮的数组,这有点糟糕——但现在就来了。好吧,使用jQuery插件读取cookie有点傻,尽管我用同一个插件做了同样的事情 有一些简短的代码,您可以使用
所有这些的核心都是document.cookie——它们只是一个字符串,而不是一个漂亮的数组,这有点让人讨厌——但就这样吧。有很多。对于初学者,您可以删除
removeCookie()
。另外,您将看到$.cookie()
方法接受值和选项。如果您只是在阅读cookies,那么这些都是不必要的。您还可以删除与它们相关的代码。我猜你可以把它归结为这样的事情:
/*!
* jQuery Cookie Plugin v1.3
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2011, Klaus Hartl
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
(function ($, document, undefined) {
var pluses = /\+/g;
function raw(s) {
return s;
}
function decoded(s) {
return decodeURIComponent(s.replace(pluses, ' '));
}
var config = $.cookie = function (key, value, options) {
// read
var decode = config.raw ? raw : decoded;
var cookies = document.cookie.split('; ');
for (var i = 0, l = cookies.length; i < l; i++) {
var parts = cookies[i].split('=');
if (decode(parts.shift()) === key) {
var cookie = decode(parts.join('='));
return config.json ? JSON.parse(cookie) : cookie;
}
}
return null;
};
config.defaults = {};
})(jQuery, document);
/*!
*jQueryCookie插件v1.3
* https://github.com/carhartl/jquery-cookie
*
*版权所有2011,Klaus Hartl
*MIT或GPL版本2许可下的双重许可。
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
(函数($,文档,未定义){
var pluses=/\+/g;
函数原始值{
返回s;
}
已解码功能{
返回组件(替换(加上“,”);
}
var config=$.cookie=函数(键、值、选项){
//阅读
var decode=config.raw?raw:已解码;
var cookies=document.cookie.split(“;”);
对于(变量i=0,l=cookies.length;i
根据您的用例,您可以删除与config
decode
、raw
和json
相关的所有代码,只需将其设置为默认返回所需的格式。有很多。对于初学者,您可以删除removeCookie()
。另外,您将看到$.cookie()
方法接受值和选项。如果您只是在阅读cookies,那么这些都是不必要的。您还可以删除与它们相关的代码
/*!
* jQuery Cookie Plugin v1.3
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2011, Klaus Hartl
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
(function ($, document, undefined) {
var pluses = /\+/g;
function raw(s) {
return s;
}
function decoded(s) {
return decodeURIComponent(s.replace(pluses, ' '));
}
var config = $.cookie = function (key, value, options) {
// read
var decode = config.raw ? raw : decoded;
var cookies = document.cookie.split('; ');
for (var i = 0, l = cookies.length; i < l; i++) {
var parts = cookies[i].split('=');
if (decode(parts.shift()) === key) {
var cookie = decode(parts.join('='));
return config.json ? JSON.parse(cookie) : cookie;
}
}
return null;
};
config.defaults = {};
})(jQuery, document);