0

I'm trying to fire the "loadedmetadata", an event from which I re-create a video tag (because I need the dimensions of the original loaded video).

I've already seen this answer from which I coded this example :

if (!$("mySelector").addEventListener) {
    $("#mySelector").addEventListener("loadedmetadata", function(e){
        alert("Fired with addEventListener!");
    }, false);
}
else {
    $("#mySelector").attachEvent("onloadedmetadata", function(e){
        alert("Fired with attachEvent!");
    });
}

With IE8, I have a beautiful "Object doesn't support this property or method". What am I doing wrong here?

Thanks in advance.

Community
  • 1
  • 1
Léo Davesne
  • 2,103
  • 1
  • 21
  • 24

1 Answers1

1

You should not combine jQuery with JavaScript methods. Use the .bind() method to add the loadedmetadata event, like:

$('#myMedia').bind('loadedmetadata', function(e){
  // e is Event Object jQuery handles
  var ht = $(this).height(), wd = $(this).width();
});

Since .addEventListener() was standardized as of IE9 and Media Events don't exist until IE9 it will never fire with .attachEvent(). Neither of those methods is a property of the Object the jQuery function $() returns anyways.

If the above solution does not work you should look at this:

jQuery, checking to see if video has height/width

Community
  • 1
  • 1
StackSlave
  • 10,613
  • 2
  • 18
  • 35
  • Thank you for your answer. Sadly, IE8 doesn't fire the event and .width()/.height() doesn't work neither when my video is loaded. – Léo Davesne Oct 30 '13 at 13:37