Users Online

· Guests Online: 84

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Functions - JavaScript encodeURI()

JavaScript encodeURI()

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 :

  • Each instance of characters present in the URI is used for encoding the characters with the sequence of escape characters including Reserved characters, Unescaped characters, and Score.
  • The syntax flow for the encodeURI includes parameter uri which is used for representing the uri which it is pointing to for making the flow of URI streamline and customized accordingly.
  • Once an encodeURI(uri) function is declared for URI and then URI it is much needed to declare URI for decoding which involves decodeURI() function.
  • There is quite a flexible compatibility in terms of browser support with encodeURI(uri) function containing the parameter.
  • There is return type whenever encodeURI function is called that returns value as a string representing the encoded URI.
  • Moreover, any URI based component whether encodeURI or any function containing URI components using encodeURIComponent().
  • Both encodeURI(uri) and encodeURIComponent(uri) are related to each other in a way that both make use of function according to requirement.
  • The complexity of JavaScript comes out to be constant as it doesn’t make much of manipulation and changes rather it just returns the value string as return type which leads with the time complexity of this as constant.
  • Sometimes a misconception is made in terms of function analysis when It comes to selecting encode URI() function as it gets sometimes confused with both encode URI() function and escape() function as both the function works almost in the same way which also aims to encode a set of string only which include function making portable string and can be transmitted in any way in a network to any other network in a computer which supports for these special of characters with ASCII values.
  • But there exists some common functionality among all these functions from encoding URI() to encodingURIComponent() function in a way where the function makes use of the characters but with exceptions of characters which can be reserved characters, Unescaped characters and Score.
  • Although it can be said that the working flow is the same as other functions but still some differences exists with the JavaScript encodingURI() and encodingURI() components.
  • The parameter uri is common for all the encoding functionality including encodeURI() function and plays a very pivotal role as it is the only component or parameter which is used for manipulating and getting the return value of the encoded string. It points to the URI pattern which needs to be encoded using encodeURI() function or the same function related to it.

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 :

  • It provides programmers the ability to encode URI with ease and simplicity as encode URI () function helps in making the entire set of characters instance to be encoded with return value as a string.
  • It provides a lot of browsers compatible with a different version of encoding URI() function.

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.

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.71 seconds
10,831,586 unique visits