Reputation: 169
I use object
tag to embed a youtube video.
<object data="http://www.youtube.com/embed/QwievZ1Tx-8" width="100%" height="315"></object>
I would like to trigger an Ajax request (So the server can update the database's view count on the video) everytime the video play.
Anybody know how to do it ?,
NOTE : Let say the user click 'play' on the video, AJAX REQUEST CALLED. But when they pause and resume the video, AJAX REQUEST DID NOT CALLED.
Upvotes: 2
Views: 522
Reputation: 29317
You can use youtube iframe api and the callback onPlayerStateChange
which calling with the event
object which have the exact event type for it. Also, keep a flag if the video is already started and call the ajax only if it didn't.
Like this:
var isStarted = false;
function onPlayerStateChange(event) {
if (event.data == 1 && !isStarted) {
// call ajax
isStarted = true;
}
}
Upvotes: 0
Reputation: 16251
you can do it in script:
$("object")[0].onplay = function () {
//AJAX REQUEST CALLED
};
OR:
<object... onplay="myFunction()">
and in the script
myFunction(){
//AJAX REQUEST CALLED
}
It happend only when the video start to play...
Upvotes: 1