标签:man type number this pre 参数 cap blog return
javascript的api本来就支持Base64,因此我们可以很方便的来进行编码和解码。
var encodeData = window.btoa("name=xiaoming&age=10")//编码
var decodeData = window.atob(encodeData)//解码。
例子:
var encodedData = window.btoa("Hello, world"); // 编码
var decodedData = window.atob(encodedData); // 解码
在各浏览器中,使用 window.btoa
对Unicode字符串进行编码都会触发一个字符越界的异常.
先把Unicode字符串转换为UTF-8编码,可以解决这个问题, 代码来自Johan Sundstr?m:
function utf8_to_b64( str ) {
return window.btoa(unescape(encodeURIComponent( str )));
}
function b64_to_utf8( str ) {
return decodeURIComponent(escape(window.atob( str )));
}
// Usage:
utf8_to_b64(‘? à la mode‘); // "4pyTIMOgIGxhIG1vZGU="
b64_to_utf8(‘4pyTIMOgIGxhIG1vZGU=‘); // "? à la mode"
//译者注:在js引擎内部,encodeURIComponent(str)相当于escape(unicodeToUTF8(str))
//所以可以推导出unicodeToUTF8(str)等同于unescape(encodeURIComponent(str))
//example
$(function () {
$(".edit-btn").on("click",function () {
var policyNo=$(this).parents(".t-r").find(".policy-no").text();
var holderName=$(this).parents(".t-r").find(".holder-name").text();
// $.ajax({
// url:"/reviseService/revise/reviseOption/"+policyNo+"/"+holderName+"",
// type:"POST",
// success:function () {
// window.location.href();
// }
// })
// window.open("/reviseService/revise/reviseOption/"+policyNo+"/"+holderName+"");
// window.open(encodeURIComponent("/reviseService/revise/reviseOption/"+window.btoa(policyNo+"/"+holderName+"")));
window.open("/reviseService/revise/reviseOption/"+ window.btoa(unescape(encodeURIComponent(policyNo+"/"+holderName+""))+""));
})
})
//例子
var url="http://localhost:8080/reviseService/revise/reviseOption/NkRZSUwxNzA1SkswMjAwMTE3MDAwMDA0OS/lvKDlhas="; var uu=url.split("reviseOption/")[1]; console.log(uu);//NkRZSUwxNzA1SkswMjAwMTE3MDAwMDA0OS/lvKDlhas= function b64_to_utf8(str) { return decodeURIComponent(escape(window.atob(str))); } var aa=b64_to_utf8(uu); console.log(aa);//6DYIL1705JK02001170000049/张八 var cc=aa.split("/"); console.log(cc);//["6DYIL1705JK02001170000049", "张八"]
参考文档:http://www.cnblogs.com/fishtreeyu/archive/2011/02/27/1966178.html
https://developer.mozilla.org/zh-CN/docs/Web/API/WindowBase64/btoa
http://www.cnblogs.com/xiao-lei/p/6064134.html
http://www.cnblogs.com/fishtreeyu/archive/2011/02/27/1966178.html
标签:man type number this pre 参数 cap blog return
原文地址:http://www.cnblogs.com/shj-com/p/7544543.html