For instance say I run an ajax call and the returned data is something like:
<article>
<h2>Howdy</h2>
<script type="text/javascript">
alert('hi');
posts[23] = 'this wont get added into the posts array';
</script>
</article>
I then insert this returned data straight into a div already on the page. None of the javascript will execute. How do I do this correctly and make it work?
I don’t have much control of what gets returned so returning json isn’t an option.
Thanks!
To execute the JS code that was inserted into your html you can use the eval() function. For example:
eval(document.getElementById('myJSDiv').innerHTML);Hope that’s what you’re looking for.