TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVASCRIPT

JavaScript encodeURI Examples

Call the encodeURI and encodeURIComponent methods to format strings for the URL bar.
EncodeURI. URLs use special syntax—for example they encode a hash sign as a character sequence "%23." Spaces can also be encoded.
In a JavaScript program, we want to encode and decode URIs when we interact with the browser's location. This does not need to be done manually (except in special cases).
EncodeURIComponent. Suppose our app creates a "component" of a URL, not an entire URL. Symbols such as the hash and ampersand should be encoded, not left in their original condition.

Tip: With encodeURIComponent, we convert valid URL characters to their encoded representation for use in a component (not a full URL).

JavaScript program that uses encodeURIComponent var part = "c# bird & fish"; // The encodeURIComponent method will encode hash signs. var result = encodeURIComponent(part); console.log("ENCODE BEORE: " + part); console.log("ENCODE AFTER: " + result); Output ENCODE BEORE: c# bird & fish ENCODE AFTER: c%23%20bird%20%26%20fish
EncodeURI example. This method is like encodeComponentURI but it does not encode certain chars like the hash symbol. These are left alone and considered part of the URL structure.
JavaScript program that uses encodeURI var part = "f# example"; // The encodeURIComponent method will not encode some things. // ... If you want those things encoded, use encodeURIComponent. var result = encodeURI(part); console.log("ENCODEURI: " + result); Output ENCODEURI: f#%20example
DecodeURI. This method does the opposite of encodeURI—it transforms an encoded URL back into a normal string format. We can use its decodeURIComponent counterpart.
JavaScript program that uses decodeURI var part = "c%23%20example"; // Use decodeURI and decodeURIComponent methods. var result1 = decodeURI(part); var result2 = decodeURIComponent(part); console.log("DECODEURI: " + result1); console.log("DECODEURICOMPONENT: " + result2); Output DECODEURI: c%23 example DECODEURICOMPONENT: c# example
Some documentation. With JavaScript, browsers implement these methods in C++ or similar languages. They are the experts. I recommend the Mozilla.org documentation on encodeURIComponent.EncodeURIComponent: mozilla.org
Performance note. In my testing, replace() calls that replace characters globally were slower than encodeURIComponent. Three replace calls were about 3 times slower than encodeURIComponent.

So: When possible, use encodeURIComponent to fix encodings. It is faster and simpler to use.

A summary. It is tempting to write "replace" calls for URLs to fix their characters. This is a bad idea. With specialized methods like encodeURI we have a faster and clearer solution.replace
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf