JavaScript URL escape Escape (), encodeURI (), and decodeURI ()

Source: Internet
Author: User

We can tell that escape () escapes all incoming strings in addition to ASCII letters, numbers, and specific symbols, so it is best not to use this method if you want to encode the URL. encodeURI () is used to encode the entire URI because the legitimate characters in the URI are not converted by encoding. The encodeURIComponent method encodes a single uricomponent (the request parameter) should be the most common, it can be described in the parameters of Chinese, special characters escaped, without affecting the entire URL.

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);   //var 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);   //var 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 () {            "javascript hello "var str1 =//var str2 = unescape (str1) ;      alert (STR2); Popup javascript Hello}       

JavaScript URL escape Escape (), encodeURI (), and decodeURI ()

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.