0

Recently I've been experimenting with dynamically generated HTML5 tags, and I came up with a problem with binding ended event on HTML5 video tag.

I used this solution found on stack but it works only with play event (only on Chrome, does not work on Firefox).

Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Marek
  • 1,413
  • 2
  • 20
  • 36

2 Answers2

3

The reason for this, is that most HTML5 events do not bubble and jQuery relies on event bubbling for event delegation, which is sad, but you can implement it if you want by using addEventListener:

//note the third parameter has to be true, it is important to work document.addEventListener('ended', function(e){ if($(e.target).is('video')){ //e.target has ended playing } }, true);

As an alternative you can use webshim which implements many HTML5 features thorugh jQuery.

In case you request a polyfill for mediaelement, webshim fixes jQuery's event delegation code for the mediaelement features:

```webshim.polyfill('mediaelement');

$(document).on('play', 'ended', function(){ //this has ended playing });

alexander farkas
  • 13,754
  • 4
  • 40
  • 41
  • Thank you, but unfortunately your addEventListener doesn't work... I will give a try to webshim now. – Marek Aug 24 '14 at 08:40
  • Unfortunately installing webshim would require to many changes since I'm using nodejs and it also I've been requested to write minimal application in terms of number of js files. – Marek Aug 24 '14 at 08:51
0

Well I have this solution that works on firefox, chrome and safari (IE not tested).

Creating an audio element :

var audio = document.createElement("audio");

Creating an eventlistener to an audio element:

audio.addEventListener('ended', function(){ alert('done'); });

Linial
  • 1,154
  • 9
  • 22
  • it doesn't give any errors, but it won't play either, could you explain how to make it play using your method? – Marek Aug 22 '14 at 22:48
  • It plays, but callback doesn't work. Thank you for your help anyway. – Marek Aug 23 '14 at 07:25
  • No... I tried all versions of Chrome including the experimental one, this callback simply doesn't work, bug was reported but it seems like they ignored it. What I did was manual callback, first I check the video duration and I set a callback after this amount of time, this callback will remove the video element. It's not a proper way, but it worked. – Marek Feb 10 '15 at 20:46