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 ...
随机推荐
- NOI Online 赛前刷题计划
Day 1 模拟 链接:Day 1 模拟 题单:P1042 乒乓球 字符串 P1015 回文数 高精 + 进制 P1088 火星人 搜索 + 数论 P1604 B进制星球 高精 + 进制 D ...
- JSR310-新日期APIJSR310新日期API(完结篇)-生产实战
前提 前面通过五篇文章基本介绍完JSR-310常用的日期时间API以及一些工具类,这篇博文主要说说笔者在生产实战中使用JSR-310日期时间API的一些经验. 系列文章: JSR310新日期API(一 ...
- 20170809-从URL输入到页面展现
从URL输入到页面展现 1.输入URL URL:统一资源定位符,是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示. URL包含以下几部分:协议.服务器名称(或IP地址).路径.参数和查询. ...
- 简单说 JavaScript中的事件委托(下)
说明 上次我们说了一些,关于 JavaScript中事件委托的 基础知识,这次我们继续来看. 解释 先来一段代码 <!doctype html> <html lang="e ...
- swoole 异步非堵塞 server/端 client/端 代码,已经测试完毕。贴代码
服务器环境 centos7.0 swoole4.3 php7.2 pcre4.8 nginx1.8 php-fpm server.php <?php class Server { pr ...
- python爬虫的数据库连接问题
1.需要导的包 import pymysql 2.# mysql连接信息(字典形式) db_config ={ 'host': '127.0.0.1',#连接的主机id(107.0.0.1是本机id) ...
- python3:input() 函数
一.知识介绍: 1.input() 函数,接收任意输入,将所有输入默认为字符串处理,并返回字符串类型: 2.可以用作文本输入,如用户名,密码框的值输入: 3.语法:input("提示信息:& ...
- AVR单片机教程——走向高层
本文隶属于AVR单片机教程系列. 在系列教程的最后一篇中,我将向你推荐3个可以深造的方向:RTOS.C++.事件驱动.掌握这些技术可以帮助你更快.更好地开发更大的项目. 本文涉及到许多概念性的内容 ...
- 正则匹配电话号码demo
public static String doFilterTelnum(String sParam) { String result = sParam; if (sParam.length() < ...
- 【Weiss】【第03章】练习3.4、3.5:有序链表求交、并
[练习3.4] 给定两个已排序的表L1和L2,只使用基本的表操作编写计算L1∩L2的过程. [练习3.5] 给定两个已排序的表L1和L2,只使用基本的表操作编写计算L1∪L2的过程. 思路比较简单,测 ...