{"id":77,"date":"2023-06-28T12:48:11","date_gmt":"2023-06-28T12:48:11","guid":{"rendered":"https:\/\/staging.brightwhiz.com\/?post_type=topic&p=77"},"modified":"2023-06-28T12:48:29","modified_gmt":"2023-06-28T12:48:29","slug":"html-url-encoding","status":"publish","type":"topic","link":"http:\/\/local.tutorials\/topic\/html-url-encoding\/","title":{"rendered":"HTML – URL Encoding"},"content":{"rendered":"\n
URL encoding, also known as percent-encoding, is a mechanism used to represent special characters, reserved characters, and non-ASCII characters within a URL. When working with HTML, it’s important to properly encode URLs to ensure that they are correctly interpreted by web browsers and servers. In this post, we will explore HTML URL encoding and provide a reference guide to help you encode URLs effectively.<\/p>\n\n\n\n
Certain characters have special meanings in URLs and must be encoded to be included as part of the URL itself. Here are some commonly used reserved characters and their corresponding URL-encoded values:<\/p>\n\n\n\n
%20<\/code><\/li>\n\n\n\n- Ampersand (
&<\/code>): %26<\/code><\/li>\n\n\n\n- Plus (
+<\/code>): %2B<\/code><\/li>\n\n\n\n- Question Mark (
?<\/code>): %3F<\/code><\/li>\n\n\n\n- Slash (
\/<\/code>): %2F<\/code><\/li>\n\n\n\n- Hash (
#<\/code>): %23<\/code><\/li>\n<\/ul>\n\n\n\nNon-ASCII Characters<\/h2>\n\n\n\n
URLs are primarily designed to handle ASCII characters. When you need to include non-ASCII characters, such as accented letters or characters from other languages, they must be encoded using UTF-8 and then percent-encoded. For example:<\/p>\n\n\n\n
\n- \u00e9:
%C3%A9<\/code><\/li>\n\n\n\n- \u00f1:
%C3%B1<\/code><\/li>\n\n\n\n- \u6f22:
%E6%BC%A2<\/code><\/li>\n<\/ul>\n\n\n\nURL Encoding in HTML<\/h2>\n\n\n\n
In HTML, you can use character entities or the encodeURIComponent()<\/code> JavaScript function to encode URLs. Here’s an example using character entities:<\/p>\n\n\n\n<a href="https:\/\/example.com\/search?q=coffee%20beans&category=food">Search for coffee beans<\/a><\/code><\/pre>\n\n\n\nIn the above example, the space in the query parameter is encoded as %20<\/code>, and the ampersand is encoded as &<\/code>.<\/p>\n\n\n\nCommon URL Encoding Examples<\/h2>\n\n\n\n
To help you with URL encoding, here are some common characters and their URL-encoded equivalents:<\/p>\n\n\n\n
\n:<\/code> (Colon): %3A<\/code><\/li>\n\n\n\n@<\/code> (At symbol): %40<\/code><\/li>\n\n\n\n$<\/code> (Dollar sign): %24<\/code><\/li>\n\n\n\n,<\/code> (Comma): %2C<\/code><\/li>\n\n\n\n=<\/code> (Equal sign): %3D<\/code><\/li>\n<\/ul>\n\n\n\nJavaScript encodeURIComponent()<\/code>:<\/h2>\n\n\n\nIn JavaScript, you can use the encodeURIComponent()<\/code> function to encode a URL component. It encodes all characters except the alphanumeric characters (A-Z<\/code>, a-z<\/code>, 0-9<\/code>), hyphen (-<\/code>), underscore (_<\/code>), period (.<\/code>), and tilde (~<\/code>).<\/p>\n\n\n\nconst searchTerm = 'coffee beans';\nconst encodedTerm = encodeURIComponent(searchTerm);\nconsole.log(encodedTerm); \/\/ Output: coffee%20beans<\/code><\/pre>\n\n\n\nThe above example demonstrates how to encode the searchTerm<\/code> variable using encodeURIComponent().<\/p>\n\n\n\nBy properly encoding URLs, you ensure that special characters and non-ASCII characters are correctly interpreted by web browsers and servers, avoiding potential issues with URL parsing and processing.<\/p>\n\n\n\n
Remember to encode URLs whenever necessary, especially when including dynamic data or user input in your URLs.<\/p>\n\n\n\n
Example usage:<\/p>\n\n\n\n
<a href="https:\/\/example.com\/search?q=coffee%20beans&category=food">Search for coffee beans<\/a><\/code><\/pre>\n\n\n\nIn the above example, the URL is properly encoded to include the space as %20<\/code> and the ampersand as &<\/code>.<\/p>\n\n\n\nURL encoding is an essential aspect of web development, allowing you to work with URLs containing special characters and non-ASCII characters reliably. Use this reference guide to ensure proper encoding and improve the robustness of your web applications.<\/p>\n","protected":false},"excerpt":{"rendered":"
URL encoding, also known as percent-encoding, is a mechanism used to represent special characters, reserved characters, and non-ASCII characters within a URL. When working with HTML, it’s important to properly encode URLs to ensure that they are correctly interpreted by web browsers and servers. In this post, we will explore HTML URL encoding and provide […]<\/p>\n","protected":false},"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":[],"categories":[2],"tags":[],"subject":[7,9],"yoast_head":"\n
HTML - URL Encoding Reference<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n