document.onload=(function localFileVideoPlayerInit(win) { ... }(window));
Ça ne peut pas marcher comme ça : avec ce code, la fonction localFileVideoPlayerInit est immédiatement exécutée et son résultat (undefined) est attribué à document.onload.
Je pense que le mieux serait de retirer le document.onload et de déplacer la balise script juste avant la fermeture du </body>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <!doctype html>
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="fr"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<title>play.it</title>
<style>
body {
padding-top: 50px;
padding-bottom: 20px;
}
video, input {
display: block;
}
input {
width: 100%;
}
.info {
background-color: aqua;
}
.error {
background-color: red;
color: white;
}
</style>
</head>
<body>
<div id="message"></div>
<input id="myinput" type="file" accept="video/*"/>
<video id="myvideo" controls autoplay></video>
<script>
(function localFileVideoPlayerInit(win) {
var URL = win.URL || win.webkitURL,
displayMessage = (function displayMessageInit() {
var node = document.querySelector('#message');
return function displayMessage(message, isError) {
node.innerHTML = message;
node.className = isError ? 'error' : 'info';
};
}()),
playSelectedFile = function playSelectedFileInit(event) {
var file = this.files[0];
var type = file.type;
var videoNode = document.querySelector('#myvideo');
var canPlay = videoNode.canPlayType(type);
canPlay = (canPlay === '' ? 'no' : canPlay);
var message = 'Can play type "' + type + '": ' + canPlay;
var isError = canPlay === 'no';
displayMessage(message, isError);
if (isError) {
return;
}
var fileURL = URL.createObjectURL(file);
videoNode.src = fileURL;
},
inputNode = document.querySelector('#myinput');
if (!URL) {
displayMessage('Your browser is not ' +
'<a href="http://caniuse.com/bloburls">supported</a>!', true);
return;
}
inputNode.addEventListener('change', playSelectedFile, false);
})(window);
</script></body></html> |
Partager