How to encode a URL in JavaScript

H

There are two JavaScript function that you can use to accomplish this

  1.  encodeURIComponent()
    • Encodes a URI component
    • Encodes special characters
    • The following characters: , / ? : @ & = + $ # are encoded
    var url="http://test.com/index.php?url"+encodeURIComponent(url);
  2. encodeURI()
    • Encode a URI
    • Encodes special characters but not (except) , / ? : @ & = + $ #
    var url="http://test.com/index.php?url"+encodeURI(url);

About the author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me