0
<div data-video="gPXSs-6qRFc"
 data-autoplay="1"
 data-loop="1"
 id="youtube-audio">
</div>
<script src="https://www.youtube.com/iframe_api"></script>
<script src="https://cdn.rawgit.com/labnol/files/master/yt.js"></script>

If I stick this in the HTML, the div becomes populated with a play button, and the corresponding Youtube video (audio) will start to play. However, if I use Javascript to stick it on the page some time after the page has loaded, the div fails to become populated (I assume this is because the scripts we've added don't run).

Using Javascript (triggered by a user action), how can I put this content onto the page and make these scripts run?

Dev Man
  • 71
  • 8
  • not sure if misunderstand the question, but the "Detecting when the script has been executed" section in https://stackoverflow.com/a/950146/1383168 might help – Slai Oct 25 '17 at 15:23
  • When injecting the div using javascript, are you also injecting the script tags? Or are they already included in the page? – alpha-before-beta Oct 25 '17 at 15:25
  • I'm injecting the whole thing. I wanted to keep it all contained within my JS file until it was ready to go on the page and run. – Dev Man Oct 25 '17 at 15:29
  • Could you try leaving in the html the youtube script only, after injecting the div and the other script –  Oct 25 '17 at 17:45

0 Answers0