Files
30-seconds-of-code/articles/s/javascript-encodeuri-encodeuricomponent.md
Angelos Chalaris 5c913d20bd Reorganize snippets
2023-05-03 21:19:02 +03:00

1.8 KiB

title, shortTitle, type, tags, author, cover, excerpt, dateModified
title shortTitle type tags author cover excerpt dateModified
What is the difference between encodeURI() and encodeURIComponent() in JavaScript? encodeURI() vs encodeURIComponent() question
javascript
browser
chalarangelo laptop-view JavaScript provides two methods for encoding characters to URL-safe strings. Do you know when to use each one? 2021-06-12T19:30:41+03:00

encodeURIComponent()

The encodeURIComponent() function encodes everything in the given string, except A-Z a-z 0-9 - _ . ! ~ * ' ( ). You should use this function if the string you are encoding is only part of a URL.

const partOfURL = 'my-page#with,speci@l&/"characters"?';
const fullURL = 'https://my-website.com/my-page?query="a%b"&user=1';

encodeURIComponent(partOfURL); // Good, escapes special characters
// 'my-page%23with%2Cspeci%40l%26%2F%22characters%22%3F'

encodeURIComponent(fullURL);  // Bad, encoded URL is not valid
// 'https%3A%2F%2Fmy-website.com%2Fmy-page%3Fquery%3D%22a%25b%22%26user%3D1'

encodeURI()

The encodeURI() function encodes everything in the given string, except A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #. You should use this function if the string you are encoding is a full URL.

const partOfURL = 'my-page#with,speci@l&/"characters"?';
const fullURL = 'https://my-website.com/my-page?query="a%b"&user=1';

encodeURI(partOfURL); // Bad, does not escape all special characters
// 'my-page#with,speci@l&/%22characters%22?'

encodeURI(fullURL);  // Good, encoded URL is valid
// 'https://my-website.com/my-page?query=%22this%25thing%22&user=1'