0

I am trying to do the opposite of this question, namely, convert a JavaScript object into the query at the end of a url. This is for a potentially huge, and completely randomized string in terms of keys/values. For example,

activity = {myKey:"randomString"}

'<img src="images/image.gif?' + activity + '">'

Would give me back:

<img src='images/image.gif?{myKey:"randomString"}'>
1

2 Answers 2

1
var queryValues={
  param:"0",
  s:"stack",
  o:"overflow",
  myKey:"randomString"
};
var qparts=Object.keys(queryValues).map(function(k){
  return [k,queryValues[k]].map(encodeURIComponent).join("=");
});
var queryString=qparts.length?"?"+qparts.join("&"):"";


/*
queryString:
?param=0&s=stack&o=overflow&myKey=randomString
*/

Then modify the original url by concatenating with the queryString. Like so:

images/image.gif?param=0&s=stack&o=overflow&myKey=randomString

Also, from your question, you wanted a url that looks like this:

images/image.gif?{myKey:"randomString"}

Which appears malformed and not quite the opposite of what was posted in the other question you linked to. So I gave you a snippet to produce a well-formed query string instead.

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

Comments

0
var myURL = '<img src="images/image.gif?' + JSON.stringify(activity) + '">';

2 Comments

You'll actually want to do encodeURIComponent(JSON.stringify(activity)).
What he said, in terms of encoding but in particular what problem are you trying to solve?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.