Created: December/2001

How to Encode URLs
An ASCII to URLEncoded JavaScript

You know how there can't be a space in a url? Well, if you encode it, it's not a space anymore. It turns into %20 - so that the URL works across the internet, where URLs don't get to have spaces in them. There are many other exotic characters as well - and they need to be encoded. In JavaScript you use the escape() function which is as old as JavaScript itself. More recently this has been deprecated in favor of encodeURI(). On the server side, in Cold Fusion you use URLEncodedFormat(), in php you use rawurlencode() and in ASP you use Server.URLEncode().

For a nice bookmark of the ASCII table, bookmark https://www.asciitable.com/

As a demonstration of the JavaScript method, here's a silly script which prints the ASCII number, the character, and the encoded format side by side for ASCII codes 32 to 256.


Here's the source:

<pre>
<script type="text/javascript" language="JavaScript">
for(i=32;i<256;i++) {
        document.write(String.fromCharCode(i));
        document.write(' ');
        document.write(escape(String.fromCharCode(i)));
        document.write('\n');
        }
</script>
</pre>