coding++:js实现基于Base64的编码及解码
1):引入 jquery.cookie.js
/*!
* jQuery Cookie Plugin v1.4.1
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2006, 2014 Klaus Hartl
* Released under the MIT license
*/
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD (Register as an anonymous module)
define([ 'jquery' ], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}
(function($) { var pluses = /\+/g; function encode(s) {
return config.raw ? s : encodeURIComponent(s);
} function decode(s) {
return config.raw ? s : decodeURIComponent(s);
} function stringifyCookieValue(value) {
return encode(config.json ? JSON.stringify(value)
: String(value));
} function parseCookieValue(s) {
if (s.indexOf('"') === 0) {
// This is a quoted cookie as according to RFC2068,
// unescape...
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g,
'\\');
} try {
// Replace server-side written pluses with spaces.
// If we can't decode the cookie, ignore it, it's unusable.
// If we can't parse the cookie, ignore it, it's unusable.
s = decodeURIComponent(s.replace(pluses, ' '));
return config.json ? JSON.parse(s) : s;
} catch (e) {
}
} function read(s, converter) {
var value = config.raw ? s : parseCookieValue(s);
return $.isFunction(converter) ? converter(value) : value;
} var config = $.cookie = function(key, value, options) { // Write if (arguments.length > 1 && !$.isFunction(value)) {
options = $.extend({}, config.defaults, options); if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setMilliseconds(t.getMilliseconds() + days * 864e+5);
} return (document.cookie = [
encode(key),
'=',
stringifyCookieValue(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 result = key ? undefined : {},
// To prevent the for loop in the first place assign an empty
// array
// in case there are no cookies at all. Also prevents odd result
// when
// calling $.cookie().
cookies = document.cookie ? document.cookie.split('; ') : [], i = 0, l = cookies.length; for (; i < l; i++) {
var parts = cookies[i].split('='), name = decode(parts
.shift()), cookie = parts.join('='); if (key === name) {
// If second argument (value) is a function it's a
// converter...
result = read(cookie, value);
break;
} // Prevent storing a cookie that we couldn't decode.
if (!key && (cookie = read(cookie)) !== undefined) {
result[name] = cookie;
}
} return result;
}; config.defaults = {}; $.removeCookie = function(key, options) {
// Must not alter options, thus extending a fresh object...
$.cookie(key, '', $.extend({}, options, {
expires : -1
}));
return !$.cookie(key);
}; }));
2):引入 jquery.base64.js (这是自己写的 js )
(function ($) {
var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
$.extend({
"Base64encode": function (e) {
var t = "";
var n, r, i, s, o, u, a;
var f = 0;
e = $.Base64_utf8_encode(e);
while (f < e.length) {
n = e.charCodeAt(f++);
r = e.charCodeAt(f++);
i = e.charCodeAt(f++);
s = n >> 2;
o = (n & 3) << 4 | r >> 4;
u = (r & 15) << 2 | i >> 6;
a = i & 63;
if (isNaN(r)) {
u = a = 64
} else if (isNaN(i)) {
a = 64
}
t = t + _keyStr.charAt(s) + _keyStr.charAt(o) + _keyStr.charAt(u) + _keyStr.charAt(a)
}
return t
},
"Base64decode": function (e) {
var t = "";
var n, r, i;
var s, o, u, a;
var f = 0;
e = e.replace(/[^A-Za-z0-9+/=]/g, "");
while (f < e.length) {
s = _keyStr.indexOf(e.charAt(f++));
o = _keyStr.indexOf(e.charAt(f++));
u = _keyStr.indexOf(e.charAt(f++));
a = _keyStr.indexOf(e.charAt(f++));
n = s << 2 | o >> 4;
r = (o & 15) << 4 | u >> 2;
i = (u & 3) << 6 | a;
t = t + String.fromCharCode(n);
if (u != 64) {
t = t + String.fromCharCode(r)
}
if (a != 64) {
t = t + String.fromCharCode(i)
}
}
t = $.Base64_utf8_decode(t);
return t
},
"Base64_utf8_encode": function (e) {
e = e.replace(/rn/g, "n");
var t = "";
for (var n = 0; n < e.length; n++) {
var r = e.charCodeAt(n);
if (r < 128) {
t += String.fromCharCode(r)
} else if (r > 127 && r < 2048) {
t += String.fromCharCode(r >> 6 | 192);
t += String.fromCharCode(r & 63 | 128)
} else {
t += String.fromCharCode(r >> 12 | 224);
t += String.fromCharCode(r >> 6 & 63 | 128);
t += String.fromCharCode(r & 63 | 128)
}
}
return t
},
"Base64_utf8_decode": function (e) {
var t = "";
var n = 0;
var r = c1 = c2 = 0;
while (n < e.length) {
r = e.charCodeAt(n);
if (r < 128) {
t += String.fromCharCode(r);
n++
} else if (r > 191 && r < 224) {
c2 = e.charCodeAt(n + 1);
t += String.fromCharCode((r & 31) << 6 | c2 & 63);
n += 2
} else {
c2 = e.charCodeAt(n + 1);
c3 = e.charCodeAt(n + 2);
t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
n += 3
}
}
return t
}
});
})(jQuery);
3):cookie_0.1.js 封装 (这是自己写的 js)
/**
* 全局cookies操作
*/
(function ($) { /**
* 设置cookie
*/
$.setCookie = function (key, value, expires) {
/*var _key = $.base64.encode(encodeURIComponent(key));*/
if (value) {
if (typeof value == "object" || typeof value == "number") {
value = JSON.stringify(value);
}
value = $.Base64encode(value);
}
var options = {path: '/'};
if (expires) {
options.expires = expires;
}
return $.cookie(key, value, options);
}; /**
* 获取cookie
*/
$.getCookie = function (key) {
/*var _key = $.base64.encode(encodeURIComponent(key));*/
var value = $.cookie(key);
if (value) {
value = $.Base64decode(value);
value = decodeURIComponent(value);
try {
value = JSON.parse(value);
} catch (e) {
}
}
return value;
}; /**
* 清理cookie
*/
$.cleanCookie = function (key) {
$.removeCookie(key, {path: '/'});
return !$.cookie(key);
}; }(jQuery));
4):使用
CookieUtil:在 本博客 java 分类中
@RequestMapping("add")
public String add(HttpServletResponse response) {
CookieUtil.addCookie(response, "string", "string", 0);
Map<String, String> map = new HashMap<String, String>() {{
put("a", "a");
}};
CookieUtil.addCookie(response, "map", JSON.toJSONString(map), 0);
return "OK";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CookiePage</title>
<script src="/js/jquery-3.2.0.min.js"></script>
<script type="text/javascript" src="/js/cookie/jquery.cookie.js"></script>
<script src="/js/cookie/jquery.base64.js"></script>
<script type="text/javascript" src="/js/cookie/cookie_0.1.js"></script>
</head>
<body>
<script type="text/javascript"> $(function () { var _string = $.getCookie("string");
var _map = $.getCookie("map"); alert(_string + "--" + _map["a"]);
}); </script>
</body>
</html>
通过上述编写 可读|可写|可删
coding++:js实现基于Base64的编码及解码的更多相关文章
- 在LoadRunner中进行Base64的编码和解码
<Base64 Encode/Decode for LoadRunner>这篇文章介绍了如何在LoadRunner中对字符串进行Base64的编码和解码: http://ptfrontli ...
- 利用openssl进行base64的编码与解码
openssl可以直接使用命令对文件件进行base64的编码与解码,利用openssl提供的API同样可以做到这一点. 废话不多说,直接上代码了.需要注意的是通过base64编码后的字符每64个字节都 ...
- javascript中的Base64.UTF8编码与解码详解
javascript中的Base64.UTF8编码与解码详解 本文给大家介绍的是javascript中的Base64.UTF8编码与解码的函数源码分享以及使用范例,十分实用,推荐给小伙伴们,希望大家能 ...
- js和C#中的编码和解码
同一个字符串,用URL编码和HTML编码,结果是完全不同的. JS中的URL编码和解码.对 ASCII 字母和数字及以下特殊字符无效: - _ . ! ~ * ' ( ) ,/?:@&=+$# ...
- [Swift]扩展String类:Base64的编码和解码
扩展方式1: extension String { //Base64编码 func encodBase64() -> String? { if let data = self.data(usin ...
- 利用window对象自带atob和btoa方法进行base64的编码和解码
项目中一般需要将表单中的数据进行编码之后再进行传输到服务器,这个时候就需要base64编码 现在可以使用window自带的方法window.atob() 和 window.btoa() 方法进行 ...
- js与java encodeURI 进行编码与解码
JS escape()使用转义序列替换某些字符来对字符串进行编码 JavaScript 中国 编码后 JavaScript %u4E2D%u56FD unescape()对使用 encodeUR ...
- 在 Java 中如何进行 BASE64 编码和解码
BASE64 编码是一种常用的字符编码,在很多地方都会用到.JDK 中提供了非常方便的 BASE64Encoder 和 BASE64Decoder,用它们可以非常方便的完成基于 BASE64 的编码和 ...
- js实现base64编码与解码(原生js)
一直以来很多人使用到 JavaScript 进行 base64 编码解码时都是使用的 Base64.js,但事实上,浏览器很早就原生支持 base64 的编码与解码了 以前的方式 编码: <ja ...
随机推荐
- 《自拍教程36》段位三_Python面向对象类
函数只能面向过程,来回互相调用后顺序执行, 简单的编码项目,还能应付的过来, 复杂的大型项目,调用多了,就会乱. 如何才能不乱呢,可尝试下, 面向对象类的概念, 将现实世界的事物抽象成对象,将现实世界 ...
- Slog64_项目上线之ArthurSlog个人网站上线3
ArthurSlog SLog-64 Year·1 Guangzhou·China September 9th 2018 ArthurSlog Page GitHub NPM Package Page ...
- 前端每日实战:113# 视频演示如何用纯 CSS 创作一个赛车 loader
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/mGdXGJ 可交互视频 此视频是可 ...
- 为什么要使用webpack?
在网页中会引用到哪些常见的静态资源? js (.js .jsx .coffee .ts) css (.css .less .sass .scss scss是sass的plus版) imag ...
- IntelliJ IDEA神器使用技巧
说明:详情请参考慕课网课程:IntelliJ IDEA神器使用技巧:http://www.imooc.com/learn/924(感谢课程作者:闪电侠) 推荐: 1. 课程老师(闪电侠)IDEA快捷键 ...
- NSInteger打印以及字符串的转换
You can also use %zd (NSInteger) and %tu (NSUInteger) when logging to the console. NSInteger integer ...
- Redis系列五 - 哨兵、持久化、主从
问:骚年,都说Redis很快,那你知道这是为什么吗? 答:英俊潇洒的面试官,您好.我们可以先看一下 关系型数据库 和 Redis 本质上的区别. Redis采用的是基于内存的,采用的是单进程单线程模型 ...
- flask blueprint出现的坑
from flask import Blueprint admin = Blueprint('admin',__name__) def init_bule(app): app.register_blu ...
- Flink系列之1.10版流式SQL应用
随着Flink 1.10的发布,对SQL的支持也非常强大.Flink 还提供了 MySql, Hive,ES, Kafka等连接器Connector,所以使用起来非常方便. 接下来咱们针对构建流式SQ ...
- 5W2H方法:七问分析法
5W2H分析方法也叫七问分析法,是二战中美国陆军兵器修理部首创.简单.方便.易于理解.使用,富有启发意义,被广泛应用于企业管理和技术活动,对于决策和执行性的措施也非常有帮助,有助于弥补考虑问题的疏漏. ...