For some reason, a dynamically created script does not want to work ... If inserted into a manual script, the contents of the element will be displayed. Here is the script:

<div id="container" name="9bf31c7ff062936a96d3c8bd1f8f2ff3"></div> <script type="text/javascript" src="handler.js"></script> 

The most contents of the handler:

 function init() { var conts=document.getElementById("container"); var hash=conts.getAttribute("name"); var script = document.createElement("script"); conts.appendChild(script); script.type = 'text/javascript'; script.src = 'output/?hash='+hash; } init(); 

The contents of the script is not shown. But if you manually insert:

 <script type="text/javascript" src="output/?hash=9bf31c7ff062936a96d3c8bd1f8f2ff3"></script> 

That will be displayed.

  • What content should be displayed? - Zhukov Roman
  • the contents of the created script. - rimlin
  • I looked at the properties, the user agent, why sets the properties: script {display: none; } And they are not removed, even if you specify the display script element: block; Has nobody encountered this problem? Maybe I’m doing something wrong ... - rimlin
  • You are inventing a square wheel. Do as everyone through AJAX. - VladD
  • Would do it through Ajax if it were available. But it is not available .... - rimlin

1 answer 1

Probably, you run init() , the script before the page loads, respectively, it cannot find the container , exit:

 function init() { var conts = document.getElementById("container"); var script = document.createElement("script"); conts.appendChild(script); script.type = 'text/javascript'; script.src = 'https://gist.github.com/zba/4770239/raw/705211c2578c0947d3f4c6cd71f538e3a0be82b8/gistfile1.txt'; // грузим вот этот скрипт https://gist.github.com/zba/4770239#file-gistfile1-txt script.onload = function () { run(); //выполняем функцию run() из загруженного скрипта }; } window.onload = init; // ждем загрузки, и только потом выполняем. 

Demo

if you use jQuery , instead of window.onload do

 $(init); 
  • No, he finds and fills it with the necessary tag script. But the browser hangs on it for some reason script {display: none; } The contents of the loadable script are plain text in document.writeln () - rimlin
  • one
    Topstarter, if you need text to display, then it is not kosher to load it as a script - deivan_
  • I agree, but so far I can’t find another solution. - rimlin
  • The text / javascript script should be launched and not drawn - zb '12
  • one
    XMLHttpRequest exists for getting the displayed text - zb '12