I have a JSON string stored in a data attribute.
{
"active": true,
"icons": {
"activeHeader": "ui-icon-alert"
},
"animate": {
"duration": 1000,
"always": dMethod
}
}
And I have a function which named dMethod:
function dMethod() {
alert("DONE");
}
When I try to parse the string via JSON.parse I get an error said invalid character. I check and the dMethod is defined when the parse method was running and if I removed the ,"always":dMethod part then the parser worked correctly.
I can't use quotation marks around the dMethod because then the type will be string type instead of object function.
Any help would be appreciated.
Thanks,
Péter
EDIT:
Thanks you for all the answers. I make some clarification so maybe better you understand the problem. I make a really simple js library to make jqueryui unobstructive:
var juiObjects = ["accordion", "autocomplete", "button", "datepicker", "dialog", "menu", "progressbar", "slider", "spinner", "tabs", "tooltip"];
$(document).ready(function() {
for (var i = 0; i < juiObjects.length; i++) {
var attributeName = "data-" + juiObjects[i];
$("["+ attributeName + "]").each(function () {
var optionsValue = $(this).attr(attributeName);
var options = JSON.parse(optionsValue);
$(this)[juiObjects[i]](options);
});
}
});
I had to choice between JSON.parse and eval. But I think eval wouldn't be so good choice. And try to keep the "library" as simple as possible. But it looks like I have sparete the code along the widgets.