0

Possible Duplicate:
querystring encoding of a javascript object

I'm trying to make a request to a website and embed the result as an iframe in the page using javascript. I need to pass a number of parameters in the request as querystring variables and I'd like to be able to specify the parameters as an object, passing them to a function to produce a querystring so they are easy to read, maintain and manipulate.

How can I construct the querystring of a URL from a JSON object with simple values? I would expect this example:

{
    h:300,
    w:300,
    skip:500,
    count:50
}

to produce the following querystring:

h=300&w=300&skip=500&count=50

Are there any existing library functions to do this, or is the best thing to loop over the properties myself?

4
  • If you're using jQuery, .serialize() works pretty well. Commented Jan 7, 2013 at 14:28
  • It's close to being a duplicate. I'd suggest the difference is that I'm willing to use any libraries where the related question is not. Commented Jan 7, 2013 at 14:35
  • His adversity to using the answers doesn't make them any less valid for you. Check out his answers and you have yours. oleq's answer is the second answer there. Commented Jan 7, 2013 at 14:38
  • Right you are - I didn't go beyond the accepted answer on my first read. Commented Jan 7, 2013 at 14:39

2 Answers 2

4

jQuery has a built-in method for this: jQuery.param()

Sign up to request clarification or add additional context in comments.

Comments

0

I recommend you use jQuery's param(). Here is the documentation http://api.jquery.com/jQuery.param/

You would pass your object like so:

var myObj = { h:300, w:300, skip:500, count:50 }

console.log('=>', $.param(myObj) ); // prints => h=300&w=300&skip=500&count=50

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.