Search code examples
javascriptdom-eventsvideo-streaminginnerhtmlvideo-processing

Javascript. put script in div using getElementById


Simply put, when a user clicks a javascripted text such as "480p", I want that text to activate a Javascript function that simply places into a div the script for a video player that will then load another video of a different quality.

YouTube is a prime example. Back in the day when YouTube was young if you wanted to change quality then the player would be reset.

In my Javascript function I have document.getElementById('videoplayer').innerHTML="<script language=\"JavaScript\">flowplayer(\"player\", \"swf location\", {clip:{data}}); </scr"+"ipt>";

When I do this it does overwrite the div with my data, but the script will not play/activate in the div, which would reload the video player with the new stream

Any tips?


Solution

  • why not use createElement instead?

    <html>
    <head>
    <script type="text/javascript">
    function doit() {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.innerHTML = "flowplayer(\"player\", \"swf location\", {clip:{data}});";
      document.getElementById('videoplayer').appendChild(script);
    }
    </script>
    </head>
    <body>
    <div id="videoplayer">...</div>
    <input type="button" value="doit" onclick="doit()" />
    </body>
    </html>