JavaScript URL decode | Example using built-in functions

Use JavaScript decodeURIComponent() Function to decode URL. You can also use another built-in JavaScript decodeURI() function.

JavaScript URL decode Example

HTML example code.

<!DOCTYPE html>
<html>
<body>

  <script>
    var str = "https%3A%2F%2Ftutorial.eyehunts.com%2F";

    console.log(decodeURIComponent(str));

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

Output:

JavaScript URL decode

Decoding URL parameters with JavaScript

decodeURIComponent cannot be used directly to parse query parameters from a URL. It needs a bit of preparation.

<!DOCTYPE html>
<html>
<body>

  <script>
   function decodeQueryParam(p) {
    return decodeURIComponent(p.replace(/\+/g, ' '));
  }

  console.log(decodeQueryParam('search+query%20%28correct%29'));

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

Output: search query (correct)

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.