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?

  • Nothing comes to mind, why? - user207618
  • @Other, well, everything is simplistic .... to execute code in the iframe . Suppose there is a textarea writing code in it, I click on the button -> it runs in the iframe - pepel_xD
  • Then only as you suggested. The block was executed during the parsing of the document when loading. You can get back with evil by passing a line of code inside, but this is for monsters who know a lot about eval . - user207618

0