I have this code:
<script id="toModify" >
Invalid javascript that will cause errors
</script>
<script>
var s=document.getElementById("toModify");
s.innerHTML="alert('Hi')";
</script>
And it doesn’t work.
I know there is at least two things I need to do so that it works:
- Prevent the first script to run the first time.
- Once modified, re-run the first script.
How can I do these things? Is there something else I need to do?
Use a
typeattribute so that the browser doesn’t execute a script:Just
eval()the code to execute it.Of course, dynamically generating code is fraught with peril; be very careful that you’re not introducing security holes.