URL Encode
HTML URL encoding is used to convert special characters in a URL into a format that can be transmitted over the internet. This is done to ensure that the URL is properly interpreted by web servers and browsers.
For example, if you have a URL like this:
https://www.example.com/page?name=John Doe
The space in the query parameter "name=John Doe" needs to be encoded as "%20" to be valid in a URL. So the encoded URL would look like this:
https://www.example.com/page?name=John%20Doe
To URL encode a string in HTML, you can use the encodeURI()
function in JavaScript. Here is an example:
var originalString = "Hello, World!";
var encodedString = encodeURI(originalString);
console.log(encodedString); // Output: Hello,%20World!
You can also use online URL encoding tools to quickly encode a URL. Simply paste the URL into the tool and it will encode it for you.
Here is a table showing some common special characters and their corresponding URL encoded values:
Special Character | Encoded Value |
---|---|
Space | %20 |
& | %26 |
? | %3F |
= | %3D |