Parse Javascript In AJAX Responce


When you call a XMLHttpRequest or AJAX request you get responce in four formats XML, HTML, JSON and Text.

Sometimes this responce may contain some javascript. e.g. If this responce has a alert(‘hello’) function

Than it will never be execute coz it will treated as text. So how you can parse the javascript which is in AJAX responce here is the solution

document.getElementById(‘my_div’).innerHTML = response;
var x = document.getElementById(‘my_div’).getElementsByTagName(“script”);
var h = document.getElementsByTagName(“head”)[0];
for(var i=0;i<x.length;i++)
{
var s = document.createElement(“script”);
s.type=”text/javascript”;
h.appendChild(s);
s.text=x[i].text;
}

That’s it, cheers!

Advertisements