This routine only checks if the browser supports video
tag, 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, I use the document.createElement
function.
Use the "in" operator to check if the attribute exists in the element.
Whether or not it exists, the condition will apply the best option to the browser.
The variable array
, defines index of the values separated by "|" in% with% of% with%
// 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>
Sign in - demo