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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s