---
title: "Encode URL through JavaScript"  
description: "Encode URL through JavaScript"  
author: "Anonymous User"  
published: 2021-07-20  
updated: 2023-11-28  
canonical: https://www.mindstick.com/forum/156570/encode-url-through-javascript  
category: "javascript"  
tags: ["javascript", "encryption", "url"]  
reading_time: 2 minutes  

---

# Encode URL through JavaScript

How do you securely [encode](https://www.mindstick.com/blog/51/encode-and-decode-in-dot-net) a [URL](https://www.mindstick.com/forum/436/url-redirection) using [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) such that it can be placed in a GET [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp)?

## Replies

### Reply by Aryan Kumar

In JavaScript, you can use the **encodeURIComponent()** function to encode a URL. This function takes a string as input and returns a new string in which certain characters have been replaced with their corresponding URL-encoded values.

Here's a simple example:

```plaintext
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>URL Encoding in JavaScript</title>
</head>
<body>

  <script>
    // Original URL
    var originalUrl = "https://example.com/search?q=JavaScript&lang=en";

    // Encode the URL
    var encodedUrl = encodeURIComponent(originalUrl);

    // Display the results
    console.log("Original URL:", originalUrl);
    console.log("Encoded URL:", encodedUrl);
  </script>

</body>
</html>
```

In this example, the **encodeURIComponent()** function is used to encode the original URL. The results are then displayed in the console.

Please note that **encodeURIComponent()** is typically used to encode individual components of a URL, such as query parameters, rather than the entire URL. If you want to encode the entire URL, you can still use **encodeURIComponent()**, but be aware that certain characters like **:** and **/** will not be encoded, as they are allowed in the path of a URL.

If you specifically want to encode the entire URL, you can use **encodeURI()**:

```plaintext
var encodedUrl = encodeURI(originalUrl);
```

Choose the appropriate encoding function based on your specific use case.


---

Original Source: https://www.mindstick.com/forum/156570/encode-url-through-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
