joe
joe

Reputation: 17478

How to write javascript to div that self executes

My application is retrieving content from an XML file that has javascript in it. I am taking the javascript and placing it into my DOM, but the script is not executing.

How do I write javascript onto my page that will then execute?

My app goes something like this...

var javaScriptFromFile = getJS();
myDiv.innerHTML = javaScriptFromFile;
//Next, how do I execute the javascript? 
//Can I make the javascript in the innerHTML self executing?

Upvotes: 0

Views: 152

Answers (2)

VeXii
VeXii

Reputation: 3109

put it in a script tag insted of a div. but are you sure this is really what you want?

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

You could eval the javascript. Or wrap in <script> tags before putting in the innerHTML.

Upvotes: 1

Related Questions