JavaScript makes simple cryptographic processing of parameters in URLs

Source: Internet
Author: User

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.

Here is a concrete example of how to transcode the parameters in the URL and get the decoded parameters

If the URL to jump = "stu_info.html?name=xiaoming&age=10"

transcoding: url = "stu_info.html?" +window.btoa ("name=xiaoming&age=10");

Jump: window.open (URL) or window.locaton.href = URL;

Decoding: We first get the parameter list from the URL when decoding,

We can use var paramsstring = Window.location.search to get the contents of the URL (the query part of the URL), i.e. "? name=xiaoming&age=10";

Then remove the paramsstring = paramsstring.substring (1)//"name=xiaoming&age=10"

Remove & paramsstring = Paramsstring.split ("&");//["Name=xiaoming", "age=10"]

It is important to note that the encoding method in this WINDOW.BTOA does not directly work on Unicode strings. Only Ascci strings or binary data can be converted to BASE64 encoded strings. If you want to encode Unicode characters, you can do the following conversions.

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

var decodedata = window.decodeuricomponent (Window.atob (Encodedata))//decoding.

JavaScript makes simple cryptographic processing of parameters in URLs

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.