Computer >> Computer tutorials >  >> Programming >> Javascript

How to encode a URL using JavaScript function?


To encode a URL in JavaScript, use the encodeURI() method.

Example

You can try to run the following code to encode a URL −

<!DOCTYPE html>
<html>
   <body>
      <button onclick="display()">Encode</button>
      <p id="demo"></p>
      <script>
         function display() {
            var encode = "welcome msg.jsp?name=åmit&sub=programming";
            var result = encodeURI(encode);
            document.getElementById("demo").innerHTML = result;
         }
      </script>
   </body>
</html>