I want to dynamically write (and have evaluated) a script I write to a tag. Why won’t this code work?
<html>
<head>
<script id="cojs">
</script>
<script type="text/javascript">
document.getElementById('cojs').innerHTML = 'alert("hey");';
</script>
</head>
<body>
</body>
</html>
Well script tags are evaluated when they are parsed. So since the section above is not parsed anymore after you alter it, it doesn’t work.
If your usecase allows it try the eval function: