Click here to Skip to main content
15,884,353 members
Articles / Programming Languages / Javascript

JavaScript URL encode, decode and escape

Rate me:
Please Sign up or sign in to vote.
4.76/5 (5 votes)
4 Aug 2015CPOL 35.5K   3   2
A simple utility that provides JavaScript URL encode, decode and escape functionality

This article may contain live JavaScript that has been reviewed and tested for security. If you wish to submit articles containing JavaScript please email your submissions to submit@codeproject.com.

Introduction

I was sick of having to look up the difference between urlencode, encodeURIComponent and escape, so here's a quick online tool for those who need it.

 

The Code

HTML
<script type="text/javascript">
    function Process() {

        var source = document.getElementById('text');
        var text   = "" + source.value;
        var target = document.getElementById("result");
        var result = "";

        if (document.getElementById('encode').checked === true) {
            result += "<h4>urlEncode</h4><p>" + urlencode(text) + "</p>";
            result += "<h4>encodeURI</h4><p>" + encodeURI(text) + "</p>";
            result += "<h4>encodeURIComponent</h4><p>" + encodeURIComponent(text) + "</p>";
            result += "<h4>escape</h4><p>" + escape(text) + "</p>";
        }
        else {
            result += "<h4>urldecode</h4><p>" + urldecode(text) + "</p>";
            result += "<h4>decodeURI</h4><p>" + decodeURI(text) + "</p>";
            result += "<h4>decodeURIComponent</h4><p>" + decodeURIComponent(text) + "</p>";
            result += "<h4>unescape</h4><p>" + unescape(text) + "</p>";
        }

        target.innerHTML = result;
    }

    function urlencode(text) {
        return encodeURIComponent(text).replace(/!/g,  '%21')
                                       .replace(/'/g,  '%27')
                                       .replace(/\(/g, '%28')
                                       .replace(/\)/g, '%29')
                                       .replace(/\*/g, '%2A')
                                       .replace(/%20/g, '+');
    }
    function urldecode(text) {
        return decodeURIComponent((text + '').replace(/\+/g, '%20'));
    }
</script>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
GeneralUseful Pin
DrABELL4-Aug-15 11:51
DrABELL4-Aug-15 11:51 
Some time ago I was working on my IoT web app ('enRoute': Real-time NY City Bus Tracking Web App[^]). The Shuttle Bus Routes contains "+" symbol in Url, which I encoded (after couple try-errors) as %2B. So, I am testing it now using this online utility. For e.g. route S79+ Url is encoded/decoded correctly as mentioned. Good!
<lol>Life is 2short 2remove USB safely


modified 4-Aug-15 21:54pm.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.