How to encode a URL using JavaScript function?

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 29-Jan-2018 10:13:29
Advertisements