0

How to Execute JavaScript Code Loaded using Ajax.

Like If We Are Loading JavaScript Code From Server Using JavaScript.

Edited: I Don't Want To Script Tag To Interpret JavaScript Code. I'm Not Using jQuery And Any Other JavaScript Library To Do This. And I Don't Want To Invoke Any Already User-Defined Function.

I Simply Wants To Interpret The JavaScript Code Loaded From Server Using Ajax request.

3 Answers3

2

You may dynamically create an script element which has a src links to the Javascript code from serverside and insert it into the DOM tree manually.

// to create an script element.
var elemJS=document.createElement('script');

// set its src attribute to the js code from serverside
elemJS.src="http://aa.bb.cc/somescript.js";

// to find the head element.
var elemHead=document.getElementsByTagName('head')[0];

// make the script element a child node of head
elemHead.appendChild(elemJS);

// Then enjoy your serverside code
Zirak
  • 38,920
  • 13
  • 81
  • 92
0

use eval(). You have to give an ID to your script:

<script id='ajax_script'>
    //your javascript code
</script>

And then after loading your ajax response, for loading the script you add this line:

eval(document.getElementById('ajax_script').innerHTML);
Fasil kk
  • 2,147
  • 17
  • 26
0

You can use javascript's eval function. Directly or through e.g. JQuery:

jQuery.globalEval("var newVar = true;")
tbsalling
  • 4,477
  • 4
  • 30
  • 51