Click here to Skip to main content
14,029,438 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

26.3K views
3 bookmarked
Posted 4 Aug 2015
Licenced CPOL

JavaScript URL encode, decode and escape

, 4 Aug 2015
Rate this:
Please Sign up or sign in to vote.
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

<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)

Share

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...

Comments and Discussions

 
GeneralUseful Pin
DrABELL4-Aug-15 11:51
professionalDrABELL4-Aug-15 11:51 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190419.4 | Last Updated 4 Aug 2015
Article Copyright 2015 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid