I am seeking a way to run a dynamically added script on a webpage (using innerHTML
). While searching, I came across this link:
how to execute ajax output script
Although it is close to what I need, it hasn't quite solved my issue. When I make an AJAX request, I receive the following content along with other elements:
<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>
I am trying to evaluate this content after adding it to my container element (via innerHTML
):
scripts = mycontainer.querySelectorAll('script');
for (k=0;k<scripts.length;k++){
eval(scripts[k]);
}
However, I have not been successful. Do you have any suggestions? Please note that I do not want to use any libraries and I am only focusing on modern browsers without any fallbacks.
The CSS loads correctly, and the JS file exists and is properly referenced.