Skip to Content

onYouTubeIframeAPIReady()

Add applicable Youtube videos to the Nebula players object.

JavaScript January 10, 2018

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Examples

Automatically play a video when a Bootstrap modal opens.

JavaScript
jQuery(document).on('show.bs.modal', function(e){
    vidID = jQuery(e.target).find('iframe').attr('id');
    players.youtube[vidID].playVideo();
});

Additional Notes

This function is called via the Google Youtube Iframe API when it is ready.

Source File

Located in /assets/js/nebula.js on line 4651.

JavaScript
function onYouTubeIframeAPIReady(e){
    window.performance.mark('(Nebula) Loading Youtube Videos [Start]');
    jQuery('iframe[src*="youtube"]').each(function(i){
        if ( !jQuery(this).hasClass('ignore') ){ //Use this class to ignore certain videos from tracking
            //If this iframe is using a data-src, make sure the src matches
            if ( jQuery(this).attr('src').indexOf('youtube') < 0 ){ //If the src does not contain "youtube"
                jQuery(this).attr('src', jQuery(this).attr('data-src')); //Update the src to match the data-src attribute. Note: I cannot think of a better way to do this that actually works with the Youtube Iframe API
            }

            var id = jQuery(this).attr('id');
            if ( !id ){
                id = jQuery(this).attr('src').split('?')[0].split('/').pop();
                jQuery(this).attr('id', id);
            }

            if ( jQuery(this).attr('src').indexOf('enablejsapi=1') > 0 ){ //If the iframe src already has the API enabled
                nebula.addYoutubePlayer(id, jQuery(this));
                nebula.dom.document.trigger('nebula_youtube_players_created', nebula.videos[id]);
            } else {
                console.warn('The enablejsapi parameter was not found for this Youtube iframe. It has been reloaded to enable it. For better optimization, and more accurate analytics, add it to the iframe.');

                //JS API not enabled for this video. Reload the iframe with the correct parameter.
                var delimiter = ( jQuery(this).attr('src').indexOf('?') > 0 )? '&' : '?';
                jQuery(this).attr('src', jQuery(this).attr('src') + delimiter + 'enablejsapi=1').on('load', function(){
                    nebula.addYoutubePlayer(id, jQuery(this));
                    nebula.dom.document.trigger('nebula_youtube_players_created', nebula.videos[id]);
                });
            }
        }
    });
    window.performance.mark('(Nebula) Loading Youtube Videos [End]');
    window.performance.measure('(Nebula) Loading Youtube Videos', '(Nebula) Loading Youtube Videos [Start]', '(Nebula) Loading Youtube Videos [End]');

    var pauseFlag = false;
}

Override

To override or disable this JavaScript function, simply redeclare it with the exact same function name.

JavaScript
function onYouTubeIframeAPIReady(){
    //Write your own code here, leave it blank, or return false.
}