URLs, standing for Uniform Resource Locators, are a way to identify a resource on the web.
You use URLs to visit websites, point to specific files, and to give names to IP addresses.
The web supports sending data via URLs by encoding it in the URL.
The main point of encoding URLs is to make sure that the characters in the URL are safe to use.
This means that any characters that are unsafe must be escaped.
The first method,
encodeURI, takes an existing URL and encodes it safely.
const safeUrl = encodeURI(unsafeUrl);
encodeURI method encoded the space in the URL as
%20, which made the entire URL safe.
If you want to encode query parameters, you can use the
const safeUrl = "https://www.google.com/search?q=" + encodeURIComponent(unsafeParameters);
Either way, the
encodeURIComponent methods both help accomplish the same task, turning unsafe characters into safe ones, thereby encoding the URL.
Simply use either the
encodeURIComponent methods to encode a string safely.
Thanks for reading and happy coding!
- Getting Started with Solid
- Getting Started with Svelte
- How to Set Up Cron Jobs in Linux
- How to deploy a Deno app using Docker
- Getting Started with Deno
- How to deploy a Node app using Docker
- How to Scrape the Web using Node.js and Puppeteer
- Getting Started with Handlebars.js
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Getting Started with Moment.js
- Learn how to build a Slack Bot using Node.js
- Using Push.js to Display Web Browser Notifications