Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create a HTML5 video element without it being shown in the page

Tags:

Is it possible to dynamically create a HTML5 video element so that I can access the element by API's like document.getElementById or Name but it may not show up in the webpage.

Something like div.hide() or something in that direction ?

like image 903
Swaraj Chhatre Avatar asked Oct 08 '13 15:10

Swaraj Chhatre


People also ask

How do I hide a video tag in HTML?

To hide a video on a web page, use yourVariableName. style. display='none'.

How do I change the source of a video in HTML?

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.


1 Answers

You can try

var video = document.createElement('video');  video.src = 'urlToVideo.ogg'; video.autoplay = true; 

you can also use the canPlayType method to check if the browser supports the video format you want to use before setting source

if (video.canPlayType('video/ogg').length > 0) {     /* set some video source */ } 

The method returns maybe or perhaps depending on browser. If empty string it means it can't play it.

You can now use the video using the API. Just store it globally. You can later insert it into the DOM. Hope this helps.

like image 125
Mike-O Avatar answered Sep 23 '22 17:09

Mike-O