JavaScript URL Encode Example – How to Use encodeURIcomponent() and encodeURI()

preview_player
Показать описание
Text Source:

#ENCODEURI() #AND #ENCODEURICOMPONENT() #USE #HOW #EXAMPLE #ENCODE #URL #JAVASCRIPT

JavaScript URL Encode Example – How to Use encodeURIcomponent() and encodeURI()

You might think that encodeURI and encodeURIComponent do the same thing, at least from their names. And you might be confused which one to use and when.

Anything that uniquely identifies a resource is its URI, such as id, name, or ISBN number. A URL specifies a resource and how it can be accessed (the protocol). All URLs are URIs, but not all URIs are URLs.

URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded.

This means that we need to encode these characters when passing into a URL. Special characters such as &, space, ! when entered in a url need to be escaped, otherwise they may cause unpredictable situations.

User has submitted values in a form that may be in a string format and need to be passed in, such as URL fields.

encodeURI and encodeURIComponent are used to encode Uniform Resource Identifiers (URIs) by replacing certain characters by one, two, three or four escape sequences representing the UTF-8 encoding of the character.

encodeURIComponent should be used to encode a URI Component - a string that is supposed to be part of a URL.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as
Рекомендации по теме
join shbcf.ru