URL encoding, also known as percent-encoding, is a mechanism for representing special characters and non-ASCII characters in a URL. It replaces reserved characters and characters outside the ASCII range with a "%" symbol followed by two hexadecimal digits representing the character's ASCII code. URL encoding ensures that the URL remains valid and the characters are correctly interpreted by web browsers and servers.
In HTML, you can use the `encodeURIComponent()` function in JavaScript to URL encode a string. Here's an example:
html
<script>
var originalString = "Hello World!";
var encodedString = encodeURIComponent(originalString);
console.log(encodedString);
</script>
Output: `Hello%20World%21`
In this example, the `encodeURIComponent()` function is used to encode the string "Hello World!". The resulting encoded string replaces the space character with `%20` and the exclamation mark with `%21`.
If you want to manually URL encode a string, you can use the following rules:
1. Alphanumeric characters (A-Z, a-z, 0-9) remain unchanged.
2. Special characters such as `!`, `*`, `'`, `(`, `)`, and `.` remain unchanged.
3. Space characters are encoded as `%20` or replaced with `+`.
4. Reserved characters, such as `:`, `/`, `?`, `#`, `[`, `]`, `@`, `!`, `$`, `&`, `'`, `(`, `)`, `*`, `+`, `,`, `;`, `=`, and `%`, are encoded.
5. Non-ASCII characters are encoded using UTF-8 encoding.
Here are a few examples of URL encoding:
- Space character: ` ` is encoded as `%20` or replaced with `+`.
- Question mark: `?` is encoded as `%3F`.
- Ampersand: `&` is encoded as `%26`.
- Pound symbol: `#` is encoded as `%23`.
- Euro symbol: `€` is encoded as `%E2%82%AC`.
URL encoding is important when passing data via query parameters, form submissions, or constructing URLs dynamically to ensure that the URL remains valid and the data is correctly interpreted by the server.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc