Perhabs this questions should be named: How to animate jQuery-objects through hash.
Howsoever… How can I convert a string into an object? I’ve tried JSON.parse and $.parseJSON but get the following error-message: Uncaught SyntaxError: Unexpected token t.
What I try to accomplish: I want to animate jQuery objects through a hash.
Here’s my current state (Maybe there’s a better way? Then tell it to me!):
var params = (location.hash || '#').substr(1).split('|');
$.map(params, function (e) {
var parts = e.split('=');
$(parts[0]).animate(parts[1]);
console.log(typeof parts[1]); // 2 `string`
});
Use this fiddle and attach these hash to the url:
##foo={top:100,left:100}|#bar={top:100,right:100}
(Sorry markdown doesn’t allow me to append this kind of hash directly to the link).
Your JSON isn’t valid. (Paste it into http://jsonlint.com/ and see for yourself.)
JSON is stricter than JavaScript syntax and requires quoted object keys:
If you put quotes around
top,left, andright, then you can just useJSON.parseor$.parseJSONon the JSON strings directly.