Setting innerHTML using script inside
It looks like the tag is being <script>added as you expect, but the code inside it is not executing. The same error occurs if you try to use document.head(or any other DOM element, it seems). For some reason (possibly adhering to standards, possible security), inline code inside blocks <script>added through .innerHTMLsimply does not start.
However, I have working code that creates similar functionality:
var script = document.createElement('script');
script[(script.innerText===undefined?"textContent":"innerText")] = 'alert(1);';
document.documentElement.appendChild(script);
<script> documentElement.appendChild textContent innerText <script>.
, html innerhtml.
: https://www.owasp.org/index.php/DOM_based_XSS_Prevention_Cheat_Sheet
:
var newScript = document.createElement( "script" );
newScript.type = 'text/javascript';
var scriptContent = document.createTextNode( "googletag.cmd.push( function() { googletag.display( '" + encodeURIComponent( divID ) + "' ); } );" );
newScript.appendChild( scriptContent );
Here is an example in action: https://jsfiddle.net/BrianLayman/4nu667c9/