Functions - JavaScript encodeURI()
Posted by Superadmin on May 06 2023 02:38:30

JavaScript encodeURI()

By Anusua DuttaAnusua Dutta
  

JavaScript encodeURI()

Definition of JavaScript encodeURI()

JavaScript URI() function is a function in JavaScript which is used to encode any URI (Uniform Resource Identifier) that substitutes certain instance of character with one, two or three escape sequence representingUTF-8 pattern for the encoding of characters with the encoding URI function including special URI characters with some reserved characters representing a set of exception in characters such as ; , /? : @ & = + $ followed by another set of exception in characters namely Unescaped characters with alphabetical, decimal digits, – _ . ! ~ * ‘ () and score representing # .

Syntax:

encodeURI(uri)

The syntax flow is in a way where the encode URI function is used for encoding of URI as soon as it is called and is passed with the parameter uri which is required to represent that this specific URI needs to be encoded.

How encodeURI() Function Works in JavaScript?

EncodeURI() function is a function in JavaScript that is used for encoding any URI that can substitute a certain instance of character with a set of characters with certain exceptions. Let’s see the working flow of encodeURI() function in JavaScript :

Examples of JavaScript encodeURI()

Following are the examples as given below:

Example #1

This program demonstrates the JavaScript encodeURI() function which is used for encoding the string given as an input on clicking the button as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Press on button to get insight for the URI.</p>
<button onclick="Func_p()">Run</button>
<p id="demo_o"></p>
<script>
function Func_p() {
varur_i = "anusua@dutt56!meet@sometime";
var res_9 = encodeURI(ur_i);
document.getElementById("demo_o").innerHTML = res_9;
}
</script>
</body>
</html>

Output:

JavaScript encodeURI()-1.1

Example #2

This program demonstrates the comparison or minute differences existing when compared with the JavaScript encodeURI() function and JavaScript encodeURIComponent() function.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Press on the button to encode an URI.</p>
<button onclick="Func_k()">Run</button>
<p id="demo_8"></p>
<script>
function Func_k() {
varur_i = "https://jhuthigh.test.98.ghty.com";
varr_l = encodeURIComponent(ur_i);
document.getElementById("demo_8").innerHTML = r_l;
}
</script>
</body>
</html>

Output:

JavaScript encodeURI()-1.2

Example #3

This program demonstrates the clear distinction and difference between the JavaScript encode URI() and JavaScript escape() function as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<script>
document.write(escape("Access help in . Get help from www.educba.com !"));
</script>
</body>
</html>

Output:

difference-1.3

Example #4

This program demonstrates the decode URI function in corporation to encode URI function and the functionality difference between the two as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Press the button to decode a URI once it is encoded.</p>
<button onclick="Func_g()">Run</button>
<p id="demo_7"></p>
<script>
function Func_g() {
varur_i = "www.khit.asp?ger89383_))))^./";
varen_c = encodeURI(ur_i);
varde_c = decodeURI(en_c);
var res_0 = "Encoded URI: " + en_c + "<br>" + "Decoded URI: " + de_c;
document.getElementById("demo_7").innerHTML = res_0;
}
</script>
</body>
</html>

Output:

decode URI function 1.4

Note: The main aim of all these programs demonstrated is to encode the string just like encodeURI() function of JavaScript with some mere differences in functionality calling and encoding pattern otherwise the functionality or behavior is almost the same.

Advantages

Every functionality has some advantages associated with it so do encodeURI functionality which are is as follows :

Conclusion

Unlike other functions of encoding the sequence of characters,encodeURI() function easily encodes the URI with help of the function and then provides the user the end output which helps in making the overall method of character reading a bit simple and easy to implement with the user. Also, the other methods present are quite compatible and behave in a similar way with mere and very less changes for encoding URI.