It is necessary to programmatically change the content of the <script> , and then execute the new code.
Nothing comes to mind, how to remove it and paste it into the document again.
For example:
var btn = document.querySelector('button'); btn.addEventListener('click', function() { var script = document.querySelector('script'); var newScript = document.createElement('script'); newScript.innerHTML = "document.write('document');" script.parentElement.replaceChild(newScript, script); });
<button>run new code</button> <script> console.log('concole') </script>
Are there any other methods to do this?