Unable to change source of video tag via JavaScript on IE9

南楼画角 提交于 2019-11-30 23:40:53

Remove the complete video html element and create a new one instead of just replacing one of its attributes.

Jörn Berkefeld

in contrast to TweeZz I advice against removing the entire video-element as this breaks the code on iOS (and Android).

instead do not put the source elements in your html but instead add them via JS. IE9 does not allow this which again you can catch and then edit the src-attribute of the video element itself, as I showed here: Video tag not working in IE 9

as a shortcut, here the code I posted there:

$('video').append('<source src="' + pathMp4 + '" type="video/mp4"><source src="' + pathWebm + '" type="video/webm">');
if(!$('video').children('source').length) { // set src&type attribute for ie9/android3.1 because it does not add the source child-elements
    $('video').attr('src', pathMp4 ).attr('type','video/mp4');
}

Background: when you set source-elements in your html, IE9 prioritizes this information. Later, when you use the .src() function only the src-attribute of the video-element will be overwritten but due to IE9's prioritization of the source-element this won't matter.

--> definitely a bug in IE9

UPDATE: please check this post for a better solution: Video tag not working in IE 9

FYI: you can make this work by not using the <source> elements and instead using the src attribute: <video id="example_video_2" class="video-js" width="640" height="264" controls="controls" preload="auto" > <source src="song.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' /> </video>

Just becomes <video id="example_video_2" class="video-js" width="640" height="264" controls="controls" preload="auto" src="song.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'></video>

Not sure if this was causing your problem but do you not need the closing </video> tag? I know that some closing tags can be omited in HTML5, but I didn't think video was one and it would probably cause issues with the javascript.

mkralla11

I agree with Berkefeld's advice against replacing the whole video element, BUT there is a better solution to this problem than creating the video element and appending videos via javascript as suggested.

I faced the same exact problem here and after a lot (and I mean a lot) of trial and error, I found the real solution and posted it on that page. Internet Explorer was a nightmare when dealing with changing videos via HTML5 video tags, but this solves it.

Had the same issue as the OP only with IE11. The error "Invalid Source" would crop up even when simply executing video.load() without even changing the video.src. My issue turned out that it was also related to the server ( IIS 7.5).

Switched to a newer server (Windows Server 2012 R2 w/ IIS 8.5) and the problems disappeared!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!