how to encode form GET parameters in javascript

Posted on 2012-09-22
Last Modified: 2012-09-27
Is there a javascript function to encode strings to the format for passing as <form method=get> parameters? For example:

someFunction("hello 1,2 3/4")  gives the string: "hello%20%31%2C%32%20%33%2F%34"
Question by:jmarkfoley
    LVL 82

    Accepted Solution

    'encodeURI()' and encodeURIComponent() encodes the special characters but not the regular characters.
    LVL 11

    Assisted Solution

    Here are the ups und downs between unescape, encodeURI and encodeURIComponent methods in javascript.

    I'm sure one of the methods do the trick for you.
    LVL 1

    Author Comment

    Thanks, those functions look like what I need. However, your answers have created new questions. You've given me: escape(), encodeURI(), encodeURIComponent(). Why three functions? Surely they must be for different things, or one is more obsolecent than the other? If I pick "escape()" and I making the 'Deprecated' choice?

    In examining the side-by-side chart in mcnute's reference, encodeURI() does not encode characters less than SPACE. Why would one ever use that function? Not encoding e.g. "&" would certainly mess up a parameter string.

    escape() does not encode "+", "/" or "@". encodeURIComponent() does encode these three, but does not encode "~", "!", "'", ")", "(". These seem like rather deliberate choices for specific purposes. I am unclear as to which function is appropriate for URL parameter strings. I'm leaning toward using escape() since it encodes the most, but to I run a risk with e.g. an embedded "+" in my parameter string, or a "/" in a date string?

    LVL 82

    Expert Comment

    by:Dave Baldwin
    Anything less than SPACE is a control character and not legal in a query string.  SPACE is encoded as %20 and "&" is %26 which is not less than SPACE.  I think encodeURIComponent(chr) is the most useful for your purposes.

    More info here:
    LVL 1

    Author Comment

    OK, all good info. That last link (DaveBaldwin) had some explanation that answers my question about slashes in dates being passed as parameters:

    In the "query" component of a URI (the part after a ? character), for example, "/" is still considered a reserved character but it normally has no reserved purpose, unless a particular URI scheme says otherwise. The character does not need to be percent-encoded when it has no reserved purpose.
    LVL 1

    Author Closing Comment

    Thanks. In the end, I decided to use escape() since it seemed to encode more.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
    In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now