Decoding the encoding in JavaScript

Source: Internet
Author: User

Tag: Man type number this pre parameter Cap blog return

JavaScript APIs are inherently Base64, so we can easily encode and decode them.

var encodedata = Window.btoa ("name=xiaoming&age=10")//code

var decodedata = Window.atob (encodedata)//decoding.

Example:

var encodedData = window.btoa("Hello, world"); // 编码var decodedData = window.atob(encodedData); // 解码

In each browser, the use window.btoa of encoding a Unicode string triggers an exception with a character that is out of bounds.

Converting the Unicode string to UTF-8 encoding first solves this problem, and the code comes from 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 "//Translator Note: Within the JS engine, encodeURIComponent (str) is equivalent to escape (unicodeToUTF8 (str))//So the unicodeToUTF8 (str) can be deduced Equivalent to 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+ "")));
})
})

Example

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/eight var cc=aa.split ("/"); Console.log (cc);//[" 6dyil1705jk02001170000049 "," Zhang Eight "]

  

 
参考文档: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

Decoding the encoding in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.