Reputation: 37
I am able to embed and view my YouTube videos in my website. The problem is: when MY videos finish, a YouTube screen appears with a number of OTHER PEOPLE'S videos as choices. I want the Fancybox pop-up to close when my video finishes so this doesn't happen. I have found a number of suggestions for closing the Fancybox pop-up when a YouTube video ends, but none of them work. I wonder if that's because they're written for Fancybox-1 and I'm using Fancybox-2. I've inserted the code below, but it doesn't trigger.
<script>
function onPlayerStateChange(event){
if (event.data===0){
$.fancybox.close();
}
}
</script>
Upvotes: 0
Views: 161
Reputation: 3268
I do this by adding ?rel=0
to the end of the URL which hides all related videos (more info on rel param here)
You can add ?rel=0
to each of your video URLs (manually), or have the fancybox beforeLoad function add it just before it opens: (example also sets autoplay to 1)
beforeLoad: function () {
var url = $(this.element).attr("href");
url += '?autoplay=1&rel=0';
this.href = url;
}
(Note that this code adds it to all videos and wont work if ?
is already in the URL, so you may need to adjust the code to your page)
Upvotes: 1