Skip to Content
Menu

getYoutubeTitle()

Get the title of the Youtube video (or use best fallback possible)

JavaScript February 7, 2021

Usage

JavaScript
nebula.getYoutubeTitle(target)

Parameters

target
(Required) (Object) The event target passed within the Youtube API functions
Default: None

Request or provide clarification »

Examples

JavaScript
function nebulaYoutubeError(e){
    thisVideo.title = nebula.getYoutubeTitle(e.target);
}

Additional Notes

This checks if the undocumented getVideoData() function is available from the Youtube API. Either way it tries to return the most applicable title available. The fallback stack is Youtube title, iframe title attribute, or video ID.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/video.js on line 540.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.getYoutubeTitle = function(target){
        //If getVideoData is available in the API
        if ( target.getVideoData ){
            return target.getVideoData().title;
        }
    
        //Otherwise use the iframe title attribute (if it exists)
        if ( jQuery(target.getIframe()).attr('title') ){
            return jQuery(target.getIframe()).attr('title').trim();
        }
    
        //Otherwise use the Youtube ID instead
        let youtubeID = nebula.getYoutubeID(target);
        if ( youtubeID ){
            return youtubeID;
        }
    
        return false;
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).

    JavaScript

    For non-module import functions:

    nebula.getYoutubeTitle = function(target){
        //Write your own code here, leave it blank, or return false.
    }


    For dynamically imported module function overrides:

    jQuery(window).on('load', function(){
        nebula.getYoutubeTitle = function(target){
            //Write your own code here, leave it blank, or return false.
        }
    });


    Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:

    jQuery(document).on('nebula_module_loaded', function(module){
        //Note that the module variable is also available to know which module specifically was imported
        if ( typeof nebula.getYoutubeTitle === 'function' ){
            nebula.getYoutubeTitle = function(target){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });