Files
30-seconds-of-code/blog_posts/javascript-encodeuri-encodeuricomponent.md
Isabelle Viktoria Maciohsek 9482b077ec Bake dates into articles
2021-06-13 19:52:48 +03:00

1.8 KiB

title, type, tags, authors, cover, excerpt, firstSeen, lastUpdated
title type tags authors cover excerpt firstSeen lastUpdated
What is the difference between encodeURI() and encodeURIComponent() in JavaScript? question javascript,browser chalarangelo blog_images/laptop-view.jpg JavaScript provides two methods for encoding characters to URL-safe strings. Do you know when to use each one? 2021-01-07T10:41:38+02:00 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'