Select Page

How to encode a URL in JavaScript

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);

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.