Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use javascript to dynamically change a video's source?

How can I change a video's source using JS?

<video id="myVideoTag" width="670" height="377" autoplay="true" controls="controls">     <source src="http://www.test.com/test.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'> </video> 
like image 952
March Avatar asked Sep 17 '10 03:09

March


People also ask

How do I change the src of a video tag?

To change source on HTML5 video tag with JavaScript, we an set the src property of the video element. const changeSource = (url) => { const video = document. getElementById("video"); video. src = url; video.

Can you put videos in JavaScript?

Create audio / video elements with JavaScript The video tag can be treated like any other tag in the DOM, so you can easily create one with the document. createElement("video") and then set the video's attributes like source, controls, and so on. Here's an example that adds a video inside a div with id = "myVideo" .

How do I load a video into JavaScript?

HTML Audio/Video DOM load() Method The load() method re-loads the audio/video element. The load() method is used to update the audio/video element after changing the source or other settings.

How do you give a video source in HTML?

Attribute Values The URL of the video file. Possible values: An absolute URL - points to another web site (like src="http://www.example.com/movie.ogg") A relative URL - points to a file within a web site (like src="movie.


1 Answers

Sure,

  1. You can set the src attribute on the source element:

    document.querySelector("#myVideoTag > source").src = "http://example.com/new_url.mp4" 

    Or using jQuery instead of standard DOM methods:

    $("#myVideoTag > source").attr("src", "http://example.com/new_url.mp4"​​​​)​ 
  2. Then you need to call the load method on the video element:

    videoElement.load()

like image 147
Brian Campbell Avatar answered Oct 06 '22 15:10

Brian Campbell