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 *

You May Also Like
XOR in Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are…
How To

How to configure Git proxy?

If you are working in the corporate company all the internet requests usually goes through the corporate firewall.…