Javascript to encode and decode a URL - escape & unescape
Javascript07 Dec 2010
Now a days scripting languages like Javascript and VBScript have more importance as programmer started using AJAX more and more. There are some APIs available for Javascript which provide easy and efficient constructs to process.
Did you see space (' ') in the URL when you're accessing? You wouldn't have and instead you would have seen %20 character replacing them. That was URL encoding technique. You can do the URL encoding and decoding through Javascript as we can do it through Java and other programming languages.
escape - escape is a function, to encode the parameter and return the encoded URL.
Syntax: escape (URL)
unescape - unescape is a function, to decode the parameter and return the decoded URL.
Syntax: unescape (URL)
Enter any URL or text in the first text box and click on the button to see the encoded and decoded text.
This has been tested on all the major browsers.
click here to check other Javascript programs that I've developed and explained.
Encode and decode
Did you see space (' ') in the URL when you're accessing? You wouldn't have and instead you would have seen %20 character replacing them. That was URL encoding technique. You can do the URL encoding and decoding through Javascript as we can do it through Java and other programming languages.
escape - escape is a function, to encode the parameter and return the encoded URL.
Syntax: escape (URL)
unescape - unescape is a function, to decode the parameter and return the decoded URL.
Syntax: unescape (URL)
Example
Enter any URL or text in the first text box and click on the button to see the encoded and decoded text.
Source
<input type="text" id="url1" size="60" />
<input type="text" id="url2" size="60" />
<input type="text" id="url3" size="60" />
<input type="button" onclick="encdec()" />
<script type="text/javascript">
function encdec()
{
document.getElementById("url2").value=escape(document.getElementById("url1").value);
document.getElementById("url3").value=unescape(document.getElementById("url2").value);
}
document.getElementById("url1").value=window.location.href;
</script>
This has been tested on all the major browsers.
click here to check other Javascript programs that I've developed and explained.