URL escape characters in JavaScript | Example code

URL escape characters converts characters into a format that can be transmitted over the Internet. URLs can only be sent over the Internet using the ASCII character-set.

Use the JavaScript encodeURIComponent() function to escape characters in URL.

URL escape codes for characters that must be escaped

CharacterURL Escape Codes
SPACE%20
<%3C
>%3E
#%23
%%25
+%2B
{%7B
}%7D
|%7C
\%5C
^%5E
~%7E
[%5B
]%5D
%60
;%3B
/%2F
?%3F
:%3A
@%40
=%3D
&%26
$%24

HTML Example code URL escape characters

See how the JavaScript function encodes the text.

<!DOCTYPE html>
<html>
<body>

  <script>
    var str = "https://tutorial.eyehunts.com/";

    console.log(encodeURIComponent(str));

  </script>
</body>
</html>

Output:

URL escape characters in JavaScript

White spaces in URLs

You can use the following characters or strings to represent white space in the query portion of a URL:

  • White space ( )
  • Plus sign (+)
  • URL escape code (%20)
  • String literal escape code ($20)

Do comment if you have any doubts and suggestions on this JS URL topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

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