Assigning the value of " – Quentin Jul 26 '13 at 15:34

2 Answers2

3

maybe doing something like this is better

script=document.createElement('script');
script.src='http://whatever.js';
document.getElementsByTagName('head')[0].appendChild(script); 
gezzuzz
  • 188
  • 2
  • 16
  • Yes, but how do I assign it to: document.getElementById('ID').innerHTML ? –  Jul 26 '13 at 16:15
  • why do you need to assign it to this certain element? scripts should be in the head.. – gezzuzz Jul 26 '13 at 16:19
  • Because the script outputs some text (and this text should be placed in the body). So I need a way to assign it to a variable, for example:
    –  Jul 26 '13 at 17:00
-1

If you're trying to append the script inside the DOM as a string, you have to do something like this :

var script = '<script src="http://domain.com/external.php"></scr'+'ipt>';
document.getElementById('ID').innerHTML = script;

the closing script tag will cause issues in parsing when inserted as a string because it closes the current script, concatenation will solve that.

adeneo
  • 312,895
  • 29
  • 395
  • 388