Javascript string Encoding

Source: Internet
Author: User

Encodeuri ()The function can encode a string as a URI.

This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().

The purpose of this method is to fully encode the URI. Therefore, the encodeuri () function will not escape the following ASCII punctuation marks with special meanings in the URI :;/? : @ & =+ $ ,#

 

In this example, zhogn uses encodeuri () to encode the URI:

 
<SCRIPT type = "text/JavaScript"> document. write (encodeuri ("http://www.w3school.com.cn") + "<br/>") document. write (encodeuri ("http://www.w3school.com.cn/My first/") document. write (encodeuri (",/? : @ & =+ $ # ") </SCRIPT>

Output:

 
Http://www.w3school.com.cnhttp: // www.w3school.com.cn/my%20first /,/? : @ & =+ $ #

 

 

Encodeuricomponent ()The function can encode a string as a URI component.

This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().

Other characters (such :;/? : @ & =+ $, # The punctuation marks used to separate URI components) are all replaced by one or more hexadecimal escape sequences.

 

In this example, we will use encodeuricomponent () to encode the URI:

 
<SCRIPT type = "text/JavaScript"> document. write (encodeuricomponent ("http://www.w3school.com.cn") document. write ("<br/>") document. write (encodeuricomponent ("http://www.w3school.com.cn/P 1/") document. write ("<br/>") document. write (encodeuricomponent (",/? : @ & =+ $ # ") </SCRIPT>

Output:

 
HTTP % 3A % 2f % 2fwww.w3school.com. cnhttp % 3A % 2f % 2fwww.w3school.com.cn % 2fp % 201% 2f % 2C % 2f % 3f % 3A % 40% 3D % 2B % 26% 23

 

Note the differences between the encodeuricomponent () function and the encodeuri () function. The former assumes that its parameters are part of the URI (such as the protocol, host name, path, or query string ). Therefore, the encodeuricomponent () function uses escape characters to separate the punctuation marks of each part of the URI.

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.