This routine just checks if the browser supports tag video
, and directs an alternative.
<script>
// criar o elemento <video>
var video = document.createElement("video");
// acrescentar ao elemento <body>
document.body.appendChild(video);
troca = function (link)
{
// criando array com índice do valores do <option>
var array = link.split("|");
if('controls' in video){
document.getElementById('video').innerHTML = [
'<video width="420" height="315" controls>',
'<source src="'+array[0]+'.mp4" type="video/mp4">',
'<source src="'+array[0]+'.webm" type="video/webm">',
'</video>'
]
} else {
document.getElementById('video').innerHTML = [
'<object width="420" height="315" data="'+array[1]+'">',
'<embed width="420" height="315" src="'+array[1]+'">',
'</embed>',
'</object>'
]
}
}
</script>
Explaining
Manipulating with Javascript
To create a video element, use the function document.createElement
.
Use the "in" operator to check if the attribute exists in the element.
Whether it exists or not, the condition will apply the best option to the browser.
The variable array
, defines index of the values separated by "|" in value
of option
// criar o elemento <video>
var video = document.createElement("video");
// acrescentar ao elemento <body>
document.body.appendChild(video);
troca = function (link)
{
// criando array com índice do valores do <option>
var array = link.split("|");
if('controls' in video){
document.getElementById('video').innerHTML = [
'<video width="420" height="315" controls>',
'<source src="'+array[0]+'.mp4" type="video/mp4">',
'<source src="'+array[0]+'.webm" type="video/webm">',
'</video>'
]
} else {
document.getElementById('video').innerHTML = [
'<object width="420" height="315" data="'+array[1]+'">',
'<embed width="420" height="315" src="'+array[1]+'">',
'</embed>',
'</object>'
]
}
}
select {
width:300px;
height:100%;
padding:30px;
float: right;
background-color: #FCFCFC;
}
option {
font-size: 12pt;
color: white;
padding: 10 10 10 10;
margin: 0 auto;
background-repeat: no-repeat;
background-position: center;
background-size: 196px; 110px;
width: 196px;
height: 110px;
text-align: center;
}
option:hover {
background-color: steelblue;
cursor: pointer;
}
<span id='video'> </span>
<select id="lista" onChange="troca(this.options[this.selectedIndex].value);" size="4">
<option value="https://sites.google.com/site/mplayerplugin/repositorio/big_buck_bunny|https://www.youtube.com/embed/yUQM7H4Swgw" style="background-image: url(https://sites.google.com/site/mplayerplugin/repositorio/big_buck_bunny.jpg);"></option>
<option value="https://sites.google.com/site/mplayerplugin/repositorio/madagascar_2|https://www.youtube.com/embed/y8WJG7lrHCU" style="background-image: url(https://sites.google.com/site/mplayerplugin/repositorio/madagascar_2.jpg);"></option>
<option value="https://sites.google.com/site/mplayerplugin/repositorio/procurando_dory|https://www.youtube.com/embed/ooSaxz-SHts" style="background-image: url(https://sites.google.com/site/mplayerplugin/repositorio/procurando_dory.jpg);"></option>
<option value="https://sites.google.com/site/mplayerplugin/repositorio/meu_malvado_favorito_2|https://www.youtube.com/embed/B82b77UA9qY" style="background-image: url(https://sites.google.com/site/mplayerplugin/repositorio/meu_malvado_favorito_2.jpg);"></option>
<option value="https://sites.google.com/site/mplayerplugin/repositorio/animais_cantando|https://www.youtube.com/embed/qiEwyK7l5uw" style="background-image: url(https://sites.google.com/site/mplayerplugin/repositorio/animais_cantando.jpg);"></option>
<option value="https://sites.google.com/site/mplayerplugin/repositorio/a_capela_animais|https://www.youtube.com/embed/791zd6qfqoU" style="background-image: url(https://sites.google.com/site/mplayerplugin/repositorio/a_capela_animais.jpg);"></option>
<option value="https://sites.google.com/site/mplayerplugin/repositorio/monstros_sa_2|https://www.youtube.com/embed/91WYf1fhB9U" style="background-image: url(https://sites.google.com/site/mplayerplugin/repositorio/monstros_sa_2.jpg);"></option>
<option value="https://sites.google.com/site/mplayerplugin/repositorio/peck_pocketed|https://www.youtube.com/embed/qmCWjjZketo" style="background-image: url(https://sites.google.com/site/mplayerplugin/repositorio/peck_pocketed.jpg);"></option>
<option value="https://sites.google.com/site/mplayerplugin/repositorio/equipment_these_days|https://www.youtube.com/embed/WxaAhc3jf5s" style="background-image: url(https://sites.google.com/site/mplayerplugin/repositorio/equipment_these_days.jpg);"></option>
<option value="https://sites.google.com/site/mplayerplugin/repositorio/skate|https://www.youtube.com/embed/Zlv6eSiGl1U" style="background-image: url(https://sites.google.com/site/mplayerplugin/repositorio/skate.jpg);"></option>
</select>
Access - demonstration
This check is not Youtube itself that does inside the iframe?
– fernandosavio
What I meant is that iframe goes to a Youtube page. As in the example
https://www.youtube.com/embed/iZUojirTEgM
. Doesn’t this page already have a script to handle this kind of thing? Tu tested this iframe in older browsers?– fernandosavio
Ah good, I thought I was in trouble even with Youtube.
– fernandosavio
for a while I don’t need to mess with videos, but I remember that if the browser doesn’t recognize the tag
video
he treats it as if it werediv
. Then you can add at the end of the tagvideo
thesource
being a compatible player. I’ll put this in an answer for you to see better– fernandosavio