"Go" encodeURI and decodeURI methods

Source: Internet
Author: User

Why do I need to call encodeURI two times to solve the garbled problem

12834595

Note the difference between the encodeURIComponent () function and the encodeURI () function, which assumes that its arguments are part of a URI (such as a protocol, hostname, path, or query string). Therefore, the encodeURIComponent () function escapes punctuation marks that are used to separate the various portions of the URI.

One, encodeURI ()//escape characters in a URI

Syntax: encodeURI (URI)//This is often used to solve Chinese garbled problems when coding different AJAX requests.

        var str1 = "Hello JavaScript";        var str2 = encodeURI (str1);        document.write (STR2);   

Second, decodeURI ()//decode the characters in a URI

Syntax: decodeURI (URI)

        var str1 = "Hello JavaScript";        var str2 = encodeURI (str1);        document.write (STR2);   Output%e4%bd%a0%e5%a5%bdjavascript        var str3 = decodeURI (str2);        document.write ("<br/>" + str3)  //output    Hello JavaScript

Three, encodeURIComponent ()//Escape the characters in the URI component

        var str1 = "Hello JavaScript";        var str2 = encodeURIComponent (str1);        document.write (STR2);   Output%e4%bd%a0%e5%a5%bdjavascript

Iv. decodeuricomponent ()//decode a character in a URI component

        var str1 = "Hello JavaScript";        var str2 = encodeURIComponent (str1);        document.write (STR2);   Output%e4%bd%a0%e5%a5%bdjavascript        var str3 = decodeuricomponent (str2);        document.write ("<br/>" + str3)  //output    Hello JavaScript

V, Escape ()//Encode a string

Syntax: Escape (value);

        var str = "JavaScript hello";        var str1 = Escape (str);        document.write (STR1); javascript%20%u4f60%u597d

VI, Unecape ()//decode a string encoded by the escape () function

        Window.onload = function () {            var str = "JavaScript hello";            var str1 = Escape (str);            document.write (STR1); javascript%20%u4f60%u597d            var str2 = unescape (str1);            alert (STR2); Popup JavaScript Hello        }

"Go" encodeURI and decodeURI methods

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.