How To Encode URL In JavaScript Tutorial with Examples? – POFTUT

How To Encode URL In JavaScript Tutorial with Examples?


JavaScript programming language provides different encoding functions with some little differences. These functions can be used to encode the URL. Encoding URL is useful to prevent errors, especially during transmission. Encoding URL will make the URL consist of only UTF-8 characters. For example space, Tilda, etc can be expressed in a proper way by encoding.

JavaScript Encoding Functions

JavaScript provides 3 encoding functions. These functions can be used according to the situation.

  • `escape`
  • `encodeURI()`
  • `encodeURIComponent()`

Encode with escape() Function

escape() is the simplest function that is deprecated in JavaScript version 1.5. It is mainly used to encode strings. * @ - _ + . / are exception which are not encoded by the escape() function. As escape() function is depracated use encodeURI() and encodeURIComponent() functions instead.

var char_set1 = ";,/?:@&=+$";  // Reserved Characters

var char_set2 = "-_.!~*'()";   // Unescaped Characters

var char_set3 = "#";           // Number Sign

var char_set4 = "ABC abc 123"; // Alphanumeric Characters + Space

var char_set5 = "poftut.com/about";  //Some normal URL to encode

var char_set6 = "İsmail Baydan" //Some name


escape(char_set1);

escape(char_set2);

escape(char_set3);

escape(char_set4);

escape(char_set5);

escape(char_set6);
Encode with escape() Function
Encode with escape() Function

Encode with encodeURI() Function

encodeURI() function is used to encode Uniform Resource Identifier or URI which is alternatively used for URL.

var char_set1 = ";,/?:@&=+$"; // Reserved Characters 

var char_set2 = "-_.!~*'()"; // Unescaped Characters 

var char_set3 = "#"; // Number Sign 

var char_set4 = "ABC abc 123"; // Alphanumeric Characters + Space 

var char_set5 = "poftut.com/about"; //Some normal URL to encode 

var char_set6 = "İsmail Baydan" //Some name 



encodeURI(char_set1);

encodeURI(char_set2);

encodeURI(char_set3);

encodeURI(char_set4);

encodeURI(char_set5);

encodeURI(char_set6);
Encode with encodeURI() Function
Encode with encodeURI() Function

Encode with encodeURIComponent() Function

encodeURIComponent() is another function used to encode given URL. encodeURIComponent() function is more useful in order to encode URLs. encodeURIComponent() will encode & properly which can cause jeopardize the integrity of the data.

var char_set1 = ";,/?:@&=+$"; // Reserved Characters 

var char_set2 = "-_.!~*'()"; // Unescaped Characters 

var char_set3 = "#"; // Number Sign 

var char_set4 = "ABC abc 123"; // Alphanumeric Characters + Space 

var char_set5 = "poftut.com/about"; //Some normal URL to encode 

var char_set6 = "İsmail Baydan" //Some name 



encodeURIComponent(char_set1); 

encodeURIComponent(char_set2); 

encodeURIComponent(char_set3); 

encodeURIComponent(char_set4); 

encodeURIComponent(char_set5); 

encodeURIComponent(char_set6);
Encode with encodeURIComponent() Function
Encode with encodeURIComponent() Function

LEARN MORE  What Is Difference Between URI and URL?

Leave a Comment