S Pangborn
S Pangborn

Reputation: 12729

JSON in location.hash for AJAX(J) bookmarking?

I'm building an AJAJ (AJAX with JSON) webapp with jQuery and I'd like my users to be able to bookmark a page that saves all of their setting selections for a certain part of the app. I've got quite a bit of data that needs to be saved, so I thought JSON might be the best way to save this, putting it into the location.hash.

That being said, what's the best way to get the string of data from the location.hash and convert it back to a JSON object so that it's usable inside the Javascript?

Here's what I'm thinking as far as the JSON object

http://example.com/index.html#json={'s': '2010-02-19', 'array':[1,2,3,4]}

Roland suggested that I drop the json=, successfuly cutting 5 characters out, too. So the complete location would be:

http://example.com/index.html#{'s': '2010-02-19', 'array':[1,2,3,4]}

Upvotes: 2

Views: 1846

Answers (3)

Roland Bouman
Roland Bouman

Reputation: 31961

Example assuming you have JSON support (either native or by including a JSON parse script)

var obj, text = document.location.hash;
if (text){
    obj = JSON.parse(text);
}

If the browser does not have native JSON support, you can grab a script from http://www.json.org/js.html or use some framework supported variant (like YUI's http://developer.yahoo.com/yui/json/)

Upvotes: 3

Josh Stodola
Josh Stodola

Reputation: 82483

Have a look at the history plugin, it might be relevant to your needs

Upvotes: 2

Mutation Person
Mutation Person

Reputation: 30498

You say 'quite a bit'. Can you be more specific? Bear in mind that you are limited to 2083 characters on your URL.

Personally, I would be reticent to store this sort of stuff in the command line anyway. You'll have pain dealing with URL encoding/decoding, and people can get an Idea of your data structures, and possibly hack the JSON string in the URL.

Upvotes: 2

Related Questions