When we use html dom,we normally run code such as :
document.getElementById('id').innerHTML = 'new text'
to change the contents of a tag such as <div>
However I tried this with the <script>
tag and it doesn’t seem to be working. What should I try?
document.getElementsByTagName('script')[0].innerHTML = "document.write('ok')"
Let’s say you have a collection of scripts:
var scripts = document.getElementsByTagName('script');
Executing
scripts[0].innerHTML = "document.write('ok')"
will effectively change the script’s content, but the script’s new content will not be executed.The solution is straightforward:
body
. It will be executed.With your to-be-updated script in the
body
this equals to: