[whatwg] <video> ... <script> race condition

Olli Pettay Olli.Pettay at helsinki.fi
Sat May 14 17:17:21 PDT 2011


On 05/15/2011 01:24 AM, Ojan Vafai wrote:
>
> It's unfortunate that you need to use an inline event handler instead of one
> registered via addEventListener to avoid the race condition. Exposing
> something to the platform like jquery's live event handlers (
> http://api.jquery.com/live/) could mitigate this problem in practice, e.g.
> it would be just as easy or easier to register the event handler before the
> element is created.

There is no need to use inline event handler.
One can always add capturing listener to window for example.
window.addEventListener("canplay",
   function(e) {
     if (e.target == document.querySelector('video') {
       // Do something.
     }
   }
, true);
And just do that before the <video> element occurs in the page.
That is simple, IMHO.

jQuery cannot do anything more than that. So if you add the event 
listener using jQuery after the <video> element, "canplay" may have
already fired.

(I wonder why the "Firing a simple event named e" defaults to 
non-bubbling. It makes many things harder than they should be.)


-Olli

>
> Ojan
>




More information about the whatwg mailing list