Search code examples
htmlurlurlencodeencodehtml-encode

does HTMLEncode(URLEncode(string)) always = URLEncode(HTMLEncode(string))?


I'm not sure if it's ever necessary to URL encode and HTML encode a string (well, perhaps unless you are building an html file that contains hyperlinks constructed using data pulled from a database or supplied by the user), but in any case, I wonder if the result is the same regardless of the order of operations.


Solution

  • If you mean URLEncode like JavaScript's encodeURI function then no the order doesn't matter.

    If you mean URLEncode like JavaScript's encodeURIComponent then yes the order does matter. Small example encoding the ampersand (&)

    url + html: %26amp%3B

    html + url: %26