71

Right, this seems to be poorly documented or I can't see it in the documentation. I basically want no related videos (?rel=0) using the JavaScript API.

$players[$vidIdPlaceholderRef] = new YT.Player('player_' + $vidIdPlaceholderRef, {
    height: '550',
    width: '840',
    videoId: $vidId
});

is what I have in place.

I have also tried:

$players[$vidIdPlaceholderRef] = new YT.Player('player_' + $vidIdPlaceholderRef, {
    height: '550',
    width: '840',
    videoId: $vidId + '?rel=0',
    rel : 0
});

with no luck. Does any one know of an option which can be added (tried rel : 0 with no luck )

DominikAngerer
  • 5,636
  • 4
  • 28
  • 55
Phil Jackson
  • 9,936
  • 20
  • 92
  • 127
  • It's Very useful in my case... check it https://stackoverflow.com/questions/13418028/youtube-javascript-api-disable-related-videos#new-answer – Karan Suthar Feb 04 '18 at 15:33

8 Answers8

157

"rel" is a player parameter, as specified here:

https://developers.google.com/youtube/player_parameters#rel

To add player parameters to iframe players, you need to specify the playerVars property of the second constructor argument (at the time of writing this is documented here, and on the IFrame API documentation page)

e.g.

new YT.Player('playerid', {
    height: '550',
    width: '840',
    videoID: 'video_id',
    playerVars: {rel: 0, showinfo: 0, ecver: 2}
});
Ivan Castellanos
  • 7,234
  • 1
  • 39
  • 39
Tim Wintle
  • 2,355
  • 1
  • 16
  • 15
  • 4
    Works perfectly, thanks. I wasn't able to find it in the [Youtube iframe reference page](https://developers.google.com/youtube/iframe_api_reference) – Shahar Nov 06 '14 at 09:57
  • 3
    This is no longer the case, as the behavior of 'rel' has changed, per the Aug 23, 2018 revision of the [docs](https://developers.google.com/youtube/). – cweber105 Oct 22 '18 at 17:39
  • As of Nov 11 2018 using showinfo=0?ecver=2 works @cweber105 – Ivan Castellanos Nov 12 '18 at 21:46
  • 10
    This has stopped working after September 25, 2018. More info -> https://developers.google.com/youtube/player_parameters – shutupchigo Nov 13 '18 at 23:54
  • @IvanCastellanos That doesn't work for me, at least from the JavaScript API. – Josh Powlison Oct 30 '19 at 15:08
  • 1
    @JoshPowlison Unfortunately YouTube (the company) is on purpose reducing more and more the things you can do with the embed. The only real solution right now is using one of the third-party JS libraries that have their own custom player (or create your own using the youtube API). – Ivan Castellanos Oct 31 '19 at 01:33
  • @IvanCastellanos Yeah, I figured things probably just changed since your comment. I understand YT's desire to put forward their brand and content, but I wish they at least had a paid dev option for hiding recommended videos and stuff. – Josh Powlison Oct 31 '19 at 14:54
17

The behavior of the rel player parameter has changed.

From documentation,

The behavior for the rel parameter is changing on or after September 25, 2018. The effect of the change is that you will not be able to disable related videos. However, you will have the option of specifying that the related videos shown in the player should be from the same channel as the video that was just played

So, it's no longer possible to disable related videos. Instead playerVars: {rel:0} will change the behavior of the player and shows suggestion from specified channel.

Kuu_Tamo
  • 302
  • 2
  • 10
  • 1
    I cannot find what you wrote, in the page you linked. Anyway that seems really what this parameter does now. – Simona Adriani Oct 22 '18 at 14:13
  • @SimonaAdriani this note is a bullet point from August 23, 2018. It points out the changes made for the showinfo parameter too if that helps you to find this from the documentation. – Kuu_Tamo Oct 23 '18 at 05:45
  • 2
    As of Nov 11 2018 you can {rel: 0, showinfo: 0, ecver: 2} to hide related params @user9568723 – Ivan Castellanos Nov 12 '18 at 21:47
5

You get related videos in two places: at the end of the video with the grid, and at the bottom of the video while paused. I've figured out a way to remove them at the end and make the ones at the bottom at least tolerable for most businesses.

1. Remove related videos at the end of a video

IFrame Player API: Events

To avoid showing related videos at the end of a video, I just stopped the video so it returned to showing the thumbnail and play button:

var player = new YT.Player('player', {
   height: '390',
   width: '640',
   events: {
      'onStateChange': function(event){
         switch(event.data){
            // Stop the video on ending so recommended videos don't pop up
            case 0:     // ended
               player.stopVideo();
               break;
            case -1:    // unstarted
            case 1:     // playing
            case 2:     // paused
            case 3:     // buffering
            case 5:     // video cued
            default:
               break;
         }
      }
   }
});

You could also replace player.stopVideo(); with any other code you want to run.

2. Making related videos at the bottom of a video only show your videos

IFrame Player API: YouTube Embedded Players and Player Parameters

rel=0 no longer avoids showing any related videos; now, it will still show related videos, but at least they'll only be from your channel. This changed sometime around September 25, 2018 (documentation).

By setting playerVars in YT.Player, we can at least have related videos only show our channel's videos. The documentation isn't clear that you have to have playerVars set up as an object, but you can set it up like so:

var player = new YT.Player('player', {
   ...
   playerVars:{
      rel:              0
      modestbranding:   1,       // If you're trying to remove branding I figure this is helpful to mention as well; removes the YouTube logo from the bottom controls of the player
      // color:         'white', // Can't have this and modestbranding active simultaneously (just a note in case you run into this)
   },
   ...
});

2A. Potential way to remove related videos from bottom

I may look more into it if I have the time, but here's where an answer may lie:

We can easily access the iframe object but we can't do anything with it: IFrame Player API: Accessing and modifying DOM nodes. It appears that because we'd be editing an iframe from YouTube there are security concerns (regardless of what we'd actually be doing). Ideally I could just remove the "More videos" text with player.getIframe().contentWindow.document.querySelector('.ytp-pause-overlay.ytp-scroll-min').remove(), but when I run player.getIframe().contentWindow.document I get an error SecurityError: Permission denied to access property "document" on cross-origin object.

But playerVars also has an origin value that may let us access the iframe's object anyway:

var player = new YT.Player('player', {
   ...
   playerVars:{
      origin:           'https://mywebsite.com'
   },
   ...
});

It's not working with localhost, but that may be a Chromium and Firefox thing. Perhaps this is a legitimate option on a live site; I'll update this post when/if I try that to let you know if I succeed.

Josh Powlison
  • 553
  • 7
  • 11
3

The accepted solution was not working for me. What does work is:

1) Putting the iframe in html that includes the rel parameter

<iframe id="youtube-video" width="560" height="315" 
 src="https://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&rel=0&modestbranding=1" 
 frameborder="0" enablejsapi="1" allowfullscreen></iframe>

2) Using the javascript API to attach to that existing player

var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var player;
function onYouTubeIframeAPIReady() {
  player = new YT.Player('youtube-video', {
    events: {
      'onReady': onPlayerReady,
      'onStateChange': onPlayerStateChange
    }
  });
}

function onPlayerReady(event) {
    console.log("ready");
}

function onPlayerStateChange(event) {
    console.log("state changed");
}

demo fiddle: http://jsfiddle.net/bf7zQ/195/

hallman76
  • 81
  • 1
  • 3
1

If you're using SWFObject, you simply need to do something like this:

function loadVideo() {
        var params = { allowScriptAccess: "always" }
            , atts = { id: "myvideo" }
        ;
//NOTE THE END OF THE BELOW LINE vvvvvv
        swfobject.embedSWF("https://www.youtube.com/v/[video id here]?enablejsapi=1&playerapiid=myvideo&version=3&rel=0"
         , "videoplaceholderid"
         , "768", "432", "8", null, null, params, atts);
    }

Just add rel=0 to the end of your url.

Jason
  • 47,815
  • 37
  • 122
  • 180
0

No need to code through the API,now its easily can be done by

You tube embed button -> Show more -> tickout the option 'Show suggested videos when the video finishes'

0

Here is a Quick solution:

setInterval(function(){
    if($('iframe').length > 0){
        $('iframe').each(function(){
            if($(this).hasClass('gotYou')){
                //do nothing
            }else{
                var getMySrc = $(this).attr('src');
                var newSrc = getMySrc.split('?');
                console.log(newSrc);
                var freshURL = newSrc[0]+'?rel=0&'+newSrc[1];
                console.log(freshURL);
                $(this).addClass('gotYou');
                $(this).attr('src', freshURL );         
            }
        });
    }
}, 1);

What it does it, it looks for the iframe in your document. If it finds iframe, it grabs the src of the iframe, finds the ? mark and then replaces ? by ?rel=0& . Here the goal is to out rel=0

MD. Atiqur Rahman
  • 1,675
  • 3
  • 12
  • 26
0

new YT.Player('playerid', {
    height: '550',
    width: '840',
    videoID: 'video_id',
    playerVars: {rel: 0},
});
Sabin Acharya
  • 343
  • 3
  • 9