Search code examples
htmljsondata-structuresstandards

Passing complex data over URL with grace


I'm thinking about using something like:

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

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?


Solution

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