Javascript URLEncode

Let us take a look at what would be the best approach for Encoding URL using Javascript. There are basically 3 Javascript URLEncode methods available.

Types of Javascript URLEncode

  • escape() will not encode: @*/+  :- This method is obsolete and deprecated since ECMAScript v3.  Hence do not use this approach to encoding the URL’s.
  • encodeURI() will not encode: ~!@#$&*()=:/,;?+’  : – If you need to encode the URL completely then you should use encodeURII() method. Below is the example of how to use encodeuri method
    var encodedString=encodeURI(" page url goes here.html");
    //This would result below output when we use console log
  • encodeURIComponent() will not encode: ~!*()’ :- encodeURIComponent method should be used only to encode the URL parameters/ query string values. Do not encode the actual URL using this method doing so will destroy the URL. Let us take an example of how to use an encodeURIComponent method.
    var world = "A string with symbols / characters that have special meaning?";
    var uri = '' + encodeURIComponent(world);
    //The output would result in encoding the query string value only.
    //Note: If we use encodeURIComponent('');
    // Output would be "" Hence it is not recommended to use on the URL.
Leave a Reply

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

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like