Most developers will know of URL encoding as UTF-8 encoding. This converts non-ASCII characters into a syntax of %NN where N stands for a unique numeric format. For example, an empty space converts to %20 when encoded. Nobody wants to do this by hand so developers have made encoding tools for convenience.
This is all possible with backend code and there are even free webapps that do this kinda stuff. But JS is so much easier because it can run without a page refresh and it runs multiple times on the same page.
In this tutorial I’ll explain how you can build a smooth URL encode/decode app without reliance on any backend technology. The app will operate like an Ajax tool but won’t need any server to make the translation.
Check out my live demo to see it in action.
Since this is rather simple we don’t need a lot to get started. A plain index.html file is enough for the webapp along with a styles.css stylesheet. I’m using Normalize for simplicity’s sake but you could use your own browser resets or even just work with browser defaults.
The app itself relies on two textareas and two buttons. The first textarea takes a string, most notably a URL but it could be literally any string.
Once a user adds something into the top textarea they can click either “encode” or “decode”. These two buttons run two distinct methods on the text which then gets placed as output content into the 2nd textarea.
Before we get into the JS code let’s take a quick look over the stylesheet.
Since Normalize handles resets and typography I don’t have much in the way of custom styling. I am using Montserrat as the header font hosted directly from Google Fonts.
Also I’m wrapping the entire page in a container with the class .wrap. This centers everything to a maximum of 700px. Since there’s not a whole lot of content this keeps everything centralized and easy to work with.
Next I have custom styles for the two textareas and buttons. Each textarea input uses 80% width so it reacts to a responsive browser window. I’m also using margin: 0 auto to center everything relative to the actual browser width.
I’m using inline-block to keep the buttons aligned together but extra margins & padding for space. Flat hover & active styles give more definition to the buttons so they insist upon user interaction.
If you know some rudimentary jQuery fundamentals you should be able to understand this script.
I’m using jQuery’s .on() method to attach the click event handler to both buttons. Whenever a user clicks either one it runs this function to encode or decode the string passed from the text box.
Once a link is clicked we first run e.preventDefault(). This stops the link’s HREF behavior from loading which stops potentially wonky browser behavior.
Next I’m grabbing the necessary action from the link’s class. I’m doing this with jQuery’s .attr() method which pulls the link class name(either “encode” or “decode”).
The only other variable we need is the string passed into the text area which I’m calling url. We get this with another jQuery method .val().
You’ll recognize a small logic statement which checks if the string is empty or nonexistent. This is just a basic check and it’s not even necessary for this instance, but good to have nonetheless.
This last block of code should be easy to understand if you can follow the previous snippet.
I’m checking which button was pressed and running the appropriate function. Then the resultant value is placed into the output textarea.
Notice one last block of code at the very bottom targeting this output text box. Whenever a user clicks to focus on the box I’m running the .select() method. This automatically selects all text within the box making it easier to copy.
The process of encoding & decoding URLs is commonplace when developing websites. But there are other reasons you might need to do this like when passing data into a database, or when trying to direct download a URL-encoded file.
I hope this webapp can be of use to people looking for a simple encoding or decoding tool. Also be sure to download a copy of my code to see what else you can build.