The application of encode and decode in JS _javascript skills

Source: Internet
Author: User
encodeURIComponent

Encodes a text string as a valid component of a Uniform Resource Identifier (URI).

encodeURIComponent (encodeduristring)

The required encodeduristring parameter represents an encoded URI component.

Description
The encodeURIComponent method returns an encoded URI. If you pass the encoding result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, note that if the string represents a path, such as/folder1/folder2/default.html, the slash will also be encoded. This will not be valid when the encoding result is sent as a request to the Web server. If the string contains more than one URI component, use the encodeURI method to encode it.

Escape method
Returns a coded String object that can be read on all computers.

function Escape (charstring:string): String
Parameters
Charstring
Must be selected. Any String object or text to encode.
Note
The Escape method returns a string value (Unicode format) that contains charstring content. All spaces, punctuation, accented symbols, and any other non-ASCII characters are replaced with%XX encoding, where xx equals the hexadecimal number that represents the character. For example, the space is returned as.

Characters with a character value greater than 255 are stored in%UXXXX format.

Note The escape method cannot be used to encode a Uniform Resource Identifier (URI). The encodeURI and encodeURIComponent methods should be used for encoding.
Requirements
Version 1

Please see
encodeURI Method | encodeURIComponent Method | String Object | Method

Apply to: Global Object

encodeURI Method
Returns a string that is encoded as a valid Uniform Resource Identifier (URI).

function encodeURI (uristring:string): String
Parameters
Uristring
Must be selected. A string representing the encoded URI.
Note
The encodeURI method returns an encoded URI. If the encoding result is passed to decodeURI, the initial string is returned. encodeURI does not encode the following characters: ":", "/", ";" and "?". Use encodeURIComponent to encode these characters.

Requirements
Version 5.5

Please see
decodeURI Method | decodeURIComponent method

Apply to: Global Object

encodeURIComponent Method
Returns a string of valid components encoded as a Uniform Resource Identifier (URI).

function encodeURIComponent (encodeduristring:string): String
Parameters
Encodeduristring
Must be selected. A string representing the encoded URI component.
Note
The encodeURIComponent method returns an encoded URI. If the encoding result is passed to decodeURIComponent, the initial string is returned. Because the encodeURIComponent method will encode all characters, note that if the string represents a path, such as/folder1/folder2/default.html, the slash will also be encoded so that when the string is sent as a request to the Web It will be invalid when the server is in use. If the string contains more than one URI component, use the encodeURI method to encode it.

Requirements
Version 5.5

Please see
decodeURI Method | decodeURIComponent method

Apply to: Global Object

Method
Returns the decoded string from a string object encoded with the escape method.

function (charstring:string): String
Parameters
Charstring
Must be selected. The String object or text to decode.
Note
method returns a String value containing the charstring content. All characters encoded in%XX 16 are replaced with the equivalent characters in the ASCII character set.

Characters encoded in%UXXXX format (Unicode characters) are replaced with Unicode characters in hexadecimal-encoded XXXX.

Note The method should not be used to decode the Uniform Resource Identifier (URI). Use the decodeURI and decodeURIComponent methods instead.
Requirements
Version 1

Please see
decodeURI Method | decodeURIComponent Method | Escape Method | String Object

Apply to: Global Object

decodeURI Method
Returns the encoded form of a Uniform Resource Identifier (URI).

function decodeURI (uristring:string): String
Parameters
Uristring
Must be selected. A string representing the encoded URI.
Note
Use the decodeURI method instead of obsolete methods.

The decodeURI method returns a string value.

If the uristring is invalid, urierror will occur.

Requirements
Version 5.5

Please see
decodeURIComponent Method | encodeURI method

Apply to: Global Object

decodeURIComponent Method
Returns the non-encoded form of an encoded component of a Uniform Resource Identifier (URI).

function decodeURIComponent (encodeduristring:string): String
The required encodeduristring parameter is a value that represents the encoded URI component.

Note
Uricomponent is part of a complete URI.

If the encodeduristring is invalid, a urierror is generated.

Requirements
Version 5.5

Please see
decodeURI Method | encodeURI method

[HTML] <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <title>js encode,decode</title> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/> </pead> <body> <script> a=62; function encode () {var code = document.getElementById (' code '). Value; Code = code.replace (/[\r\n]+/g, ""); Code = code.replace (/'/g, "\ \"); var tmp = Code.match (/\b (\w+) \b/g); Tmp.sort (); var dict = []; var i, t = ' '; for (var i=0; i<tmp.length; i++) {if (tmp[i)!= t) dict.push (t = tmp[i]); var len = dict.length; var ch; For (i=0 i<len; i++) {ch = num (i); Code = code.replace (New RegExp (' \\b ' +dict[i]+ ' \\b ', ' g '), CH); if (ch = = Dict[i]) dict[i] = '; } document.getElementById (' Code '). Value = ' eval (function (p,a,c,k,e,d) {e=function (c) {return (c<a? ') ': E ( parseint (C/A)) + (c=c%a) >35? String.fromCharCode (c+29): C.tostring ())};if (! "). Replace (/^/,string)) {while (c--) d[e (c)]=k[c]| | E (c); K=[function (e) {return d[e]}];e=function () {return ' \\\\w+ ' '};c=1};while (c--) if (K[c]) p=p.replace (New RegExp (' ' \\\\b ' +e (c) + ' \\\\b ', ' ' G '), k[c]); return P} ("+" "" +code+ "", "+a+", "+len+", "" + Dict.join (' | ') + "'. Split (' | '), 0,{})"; function num (c) {return (c<a? '): Num (parseint (C/A)) + ((c=c%a) >35? String.fromCharCode (c+29): c.tostring (36)); function run () {eval (document.getElementById (' Code '). Value); function decode () {var code = document.getElementById (' code '). Value; Code = code.replace (/^eval/, ""); document.getElementById (' Code '). Value = eval (code); } </script> <textarea id=code cols=80 rows=20> </textarea><br> <input Type=button onclick=e Ncode () value= encoding > <input type=button onclick=run () value= execution > <input type=button onclick=decode () value= decoding &G T </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

[/html]

For more control you can refer to the following JavaScript Global object reference manual for this 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.