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.

1

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

0

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

jQuery.globalEval("var newVar = true;")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.