Loaded html5 listeners do not work

I despaired. Why does it work:

var myVid=document.getElementById("movie");
myVid.onloadeddata=console.log("Browser has loaded the current frame");

and this is not so:

myVid.addEventListener("loadeddata", function()  {
  alert("loadeddata");
  });

tried both in firefox 27.

its my page where he screams: www.x.opteynde.com

My goal is to download the video download time.

+4
source share
4 answers
var myVid = document.getElementById("movie");
myVid.onloadeddata = console.log("Browser has loaded the current frame");

It does not do what you expect. You do not bind the function to myVid.onloadeddata, you immediately execute console.log()and set the return value for myVid.onloadeddata.

The correct way to do this is:

myVid.onloadeddata = function() {
    console.log("Browser has loaded the current frame");
}

So this explains why it outputs something in your console.

On loadeddata:

Definition loadeddata:

.

: " ".

Firefox :

Specified "type" attribute of "video/mp4" is not supported. Load of media resource fliege.mp4 failed.
HTTP load failed with status 404. Load of media resource http://www.x.opteynde.com/fliege.ogv failed.
All candidate resources failed to load. Media load paused.

, Firefox , , 404 .

P.S.
, loadeddata , "play".

+4

. , video.load() . html-video- .

+2

Carpetsmoker.

, : jQuery, jQuery?

jQuery.

jQuery CSS, . , movie, :

var myVid = $('#movie');

.

, , jQuery , :

$('#movie').on('event', function(e) { /* do work */ });

, 100% , $('#movie').on('loadeddata ' . , , jQuery .load()

, : vanilla JS, - :

var myVid = $('#movie')[0]; // much easier to read and reuse than .getElementByID
// however take note, this is more JS to run thus causing a a lil less speed
myVid.onloaddata = function() { /* do work */ };
0

, , body.onload.

window.onload = function() {
            myVideo.addEventListener('loadeddata', function() {
            alert('loadeddata');

        });
}

When the window.onload event is fired, myVideo.loadeddata is already running.

0
source

Source: https://habr.com/ru/post/1527927/


All Articles