Fuctions

  • encodeURI
    • Encode an entire URL. Preserves the domain and characters like ? and =.
    • See Mozilla docs or W3 schools.
    • Note escaped:
        A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
      
  • encodeURIComponent
    • Encode piece of a URL.
    • See source
  • decodeURI
    • Reverse an encoded URL.
  • decodeURIComponent
    • Reverse an encoded piece of a URL.

Examples

> encodeURI('https://example.com?q=Hello, world!')
"https://example.com?q=Hello,%20world!"

// If you actually want a character comma to be encoded and not skipped, you would use this and then build the URL.
> encodeURIComponent('Hello, world!')
"Hello%2C%20world!"

> decodeURI("Hello,%20world!")
"Hello, world!"

> decodeURI("https://example.com?q=Hello,%20world!")
"https://example.com?q=Hello, world!"