views:

57

answers:

5

I'm thinking about using something like:

<script src="http://www.example.com/?key={"param":"value"}"&gt;

As you can see in the example above a JSON formatted string is being passed over the src URL. The example, of course, doesn't work because both the JSON string and the script tag use double quotes.

Here are a few solutions that I can think of:

  1. Use single quotes in the script tag. The problem - doesn't look good in a page where all other tags use double quotes.

  2. Use &quot; instead of ". The problem - makes the src URL hard to read.

  3. Use an alternative to JSON data interchange format that doesn't use double quotes. The problem - I can't think of a good candidate.

What do you think?

A: 

In similar solutions, single quotes is fine, so I'd recommend that.

The good thing about this solution is that it distinguishes the vars from the HTML attributes with double quotes.

Dave Everitt
+1  A: 

You should use encodeUriComponent and then decode the value on the server (what language are you using on the server?).

Gabriel McAdams
Thanks for the great suggestion! I'm looking for a solution that doesn't use JavaScript, though.
Emanuil
I made the assumption that you were building the src using javascript (it is JSON). If you are not using javascript, then what are you using? Is this being generated on the server? What is it for? (I ask, because I may have another option for you).
Gabriel McAdams
The script tags will be generated by a flash application and will be used for embedding of website gadgets/components. Communication between flash's actionscript and javascript is possible and eventually I could use the method you suggest but I'd prefer a no javascript solution.
Emanuil
Actionscript is very very similar to JavaScript. What I suggested (encodeUriComponent) is also available in Actionscript.
Gabriel McAdams
Here is a link to adobe.com for more information on encoding uri's:http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/package.html
Gabriel McAdams
(blush) I should have known that. Thanks for your help!
Emanuil
+1  A: 

How complex does the data need to be? If it's just key-value pairs, then why not use ordinary form URL encoding?

<script src="http://www.example.com/?param=value&amp;otherparam=othervalue"&gt;
Brian Campbell
It gets complex enough. At some point it goes something like {"param":"value",{[{"param":"value","param":"value"},{"param":"value","param":"value"}]}}
Emanuil
+1  A: 

Look at the way the URLs are structured for Google Charts - they give some pretty good tips.

sealclubber
+1  A: 

Another option would be to urlencode just the JSON part. I'm not sure how you're constructing these URLs, but if it's by hand just take:

{"param":"value",{[{"param":"value","param":"value"},{"param":"value","param":"value"}]}}

And in Firebug call "escape" on that string, which gives you:

%7B%22param%22%3A%22value%22%2C%7B%5B%7B%22param%22%3A%22value%22%2C%22param%22%3A%22value%22%7D%2C%7B%22param%22%3A%22value%22%2C%22param%22%3A%22value%22%7D%5D%7D%7D

So you'd have a script URL such as:

<script src="http://www.example.com/?key=%7B%22param%22%3A%22...

Those percent hex codes get turned back into actual bytes by your web server, so you can just go ahead and JSON-parse the "key" GET parameter and have a nice hash/array nested structure.

darkporter
Thanks! That's an interesting approach. It's important, however, for the purposes I'm asking, that the URL is easy to read by humans.
Emanuil