views:

92

answers:

1

I want to check if an event is available or not before binding a function to it. The problem is that Google Chrome support the event "loadedmetadata" in the Video element while FireFox don't.

I did the following

$('video').bind('loadedmetadata', videoloaded);
videoloaded();

It worked well in Firefox but when I tried in Chrome, the function was executed twice (which is logical). I want to check if loadedmetadata event handler exists or not to run the function only one time in each browser.

If such possibility doesn't exist, any intelligent work around for this?

+1  A: 

Check the $video.data("events") if this object contains your event, since you are using .bind all events of this element will be stored in this object.

var $video = $("#video");
var $ve = $video.data("events");

// checking if a `loadedmetadata` object exists in `data("events")`
if ($ve != null && typeof($ve.loadedmetadata) !== undefined)
{
    // has loadedmetadata event
}

Full working example on jsFiddle

BrunoLM
@Peter: I've added an example of it working on jsFiddle.
BrunoLM
@Bruno - I'm slightly more confused now than when I started :(
Peter Ajtai
@Peter: Assuming he is always using jQuery to bind events, it means all eventhandlers are stored on `element.data("events")`. It is an object that contains several objects, where those contains information about the event. e.g. event `x` on an element; the `element.data("events")` will return: `({x:[{handler:(function () {}), data:(void 0), namespace:"", type:"x", guid:31}]})`
BrunoLM
Thanks Bruon, I'll have to look into this a little more.
Peter Ajtai
It doesn't work, at least in FireFox, because even if the Event doesn't exist (not implemented by the browser) it'll get binded and added to that events object. So it'll give wrong results. I have figured out a way though, with the readyState property. Thanks for your input.
Omar Abid
@Omar: `loadmetadata` is not a standard event http://www.w3schools.com/tags/ref_eventattributes.asp . I though you were binding a custom event: http://api.jquery.com/bind/
BrunoLM
it's *loadedmetadata* and not loadmetadata. But isn't Chrome only implementing standard things?
Omar Abid