setAttribute and video.src for changing video tag source not working in IE9

前端 未结 1 1357
予麋鹿
予麋鹿 2020-11-28 08:08

I have literally read every stackoverflow thread regarding changing the video tag source dynamically via javascript in IE9, including the useful but not agreed upon posts he

相关标签:
1条回答
  • 2020-11-28 08:28

    Great news, I found a true solution to switching/changing videos in HTML5 video tags via JavaScript without using the annoying hack I tried to explain! It's unbelievably simple and it just took a LOT of experimenting with IE. Below is the code in its simplest form to work in IE:

    <html>
      <body>
        <video id='videoPlayer' width="320" height="240" controls="controls">
           <source id='mp4Source' src="movie.mp4" type="video/mp4" />
           <source id='oggSource' src="movie.ogg" type="video/ogg" />
        </video>
    
    <!-- You MUST give your sources tags individual ID's for the solution to work. -->
    
        <script>
          var player = document.getElementById('videoPlayer');
    
          var mp4Vid = document.getElementById('mp4Source');
    
          player.pause();
    
          // Now simply set the 'src' property of the mp4Vid variable!!!!
    
          mp4Vid.src = "/pathTo/newVideo.mp4";
    
          player.load();
          player.play();
        </script>
      </body>
    </html>
    

    And there you have it. Unbelievably simple -- tested and working in IE8, and IE9... If you are having any problems, please let me know.

    0 讨论(0)
提交回复
热议问题