Take the 2-minute tour ×

I have file like this:

{'html' : '<div id="bla">something</div>',
 'script' : ' $().ready(function() { /* some code */});' }

I need to load it with jQuery and execute the 'script' variable. I know about $.getScript, but I need to run the script not from URL, but from part of JSON file. Otherwise I would need to do 2 heavy requests to server instead of one (javascript code is not static).

Is there a way to do so?

share|improve this question
possible duplicate : stackoverflow.com/questions/2212478/… –  yoda Aug 5 '11 at 8:02
add comment

2 Answers

up vote 2 down vote accepted

At first parse:

var obj = $.parseJSON(jsonString);

Then prepare div for HTML:

<div id="forHtml">

And run your code:

share|improve this answer
add comment

You can either use JavaScript's eval(script), or the jQuery.globalEval(script) if you need to:



share|improve this answer
add comment

Your Answer


