Note, the load
event will not be fired at chromium, chrome if default
attribute is not set at <track>
element.
Not sure if you are changing the src
of existing <track>
element or appending a new <track>
element to parent <video>
element?
The approach below appends a new <track>
element to <video>
element. Pass src
to foo
, attach load
event to track
element before appending to parent <video>
element, append to <track>
element to <video>
element, then set src
to the variable passed to foo
window.onload = function() {
// append next `track` element to `video` in 1 seconds
setTimeout(function() {
// pass `src`, `label` to set at `track` element
addTrack("/path/to/resource2", "track 1")
}, 1000);
function foo(track, src) {
track.addEventListener("load", function() {
console.log("this loads");
});
video.appendChild(track);
track.src = src;
};
function addTrack(src, tracklabel) {
var track = document.createElement("track");
track.kind = "captions";
track.srclang = "en";
track.label = tracklabel;
// set `default` attribute at `track` element
track.setAttribute("default", "default");
// pass `track` element, `src` to set at `track` to `foo`
foo(track, src);
}
addTrack("/path/to/resource1", "track 1");
}