AppDividend
Latest Code Tutorials

Javascript encodeURIComponent Function Tutorial With Example

0

Javascript encodeURIComponent Function Tutorial With Example is today’s topic. The encodeURIComponent() function encodes the Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing a UTF-8 encoding of the character. The new string representing the provided string encoded as the Uniform Resource Identifier (URI) component.

Javascript encodeURIComponent Function Tutorial

It returns a new string representing the provided string encoded as a Uniform Resource Identifier (URI) component. The syntax of encodeURIComponent is following.

encodeURIComponent(str)

It takes the required argument that is str. It is the component of the URI.

The encodeURIComponent escapes all the character except A-Z a-z 0-9 – _ . ! ~ * ‘ ( ).

The encodeURI differs from encodeURIComponent.

Let’s encode the reserved characters.

// app.js

let ex1 = ";,/?:@&=+$"

console.log(encodeURIComponent(ex1))

The output is following.

 

Javascript encodeURIComponent Function Tutorial With Example

Okay, now let’s see the unescaped characters.

// app.js

let ex2 = "-_.!~*'()"
console.log(encodeURIComponent(ex2))

The output is following.

 

Javascript encodeURIComponent Function

Now, let’s encode the number sign. See the following example.

// app.js

let ex3 = "#"
console.log(encodeURIComponent(ex3))

The output is following.

 

encodeURIComponent

Also, we can encode the Alpha Numeric characters plus space. See the following example.

// app.js

let ex4 = "AppDividend appdividend 21"
console.log(encodeURIComponent(ex4))

The output is following.

 

Javascript encodeURIComponent

Use encodeURIComponent on user-entered fields from forms POST’d to the server. This will encode “&” symbols that may inadvertently be generated during data entry for individual HTML entities or other characters that require encoding/decoding.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.