I want to execute a script added dynamically to the page (via innerHTML) I found this:
how to execute ajax output script
Which is close, but no cigar. I’m getting one of these (along with other content) in my AJAX response:
<link rel=StyleSheet href="/my/css/file.css" type="text/css"/>
<h1>Some header</h1>
<div>Some text</div>
<ul><li>Some text</li><li>Some Text</li></ul>
<script src='my/js/file.js'></script>
And I’m attempting to eval it after it’s appended to my container element (via innerHTML):
scripts = mycontainer.querySelectorAll('script');
for (k=0;k<scripts.length;k++){
eval(scripts[k]);
}
But to no avail. Any ideas? No libraries, please! (Don’t need fallbacks either, only most modern browsers).
CSS loads just fine. JS file exists and is correctly referenced.
Unless someone comes up with a better way…
I’m going to end up using an AJAX call to get each script src, and eval the return. I’ll post the relevant code here in a bit.