0

I'm trying to inject a script into the DOM but the script is not actually ran. What am I doing wrong? The console.log is not executed.

document.getElementById('container').innerHTML = '<div class="x"><script type="application/javascript">console.log("a");</script></div>';
2
  • Why do you need to do this? And give a demo so that we can easily figure out the problem Commented Apr 23, 2015 at 10:37
  • Did you try this? : document.getElementById('container').innerHTML = '<div id="x"></div>'; document.getElementById('x').innerHTML = '<script type="application/javascript">console.log("a");</script>'; Commented Apr 23, 2015 at 10:39

1 Answer 1

0

You can use eval() method to run dynamic scripts. But it is not recommended to run script dynamically due to security issues.

Sign up to request clarification or add additional context in comments.

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.