Skip to Content
Menu

getYoutubeID()

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

JavaScript February 7, 2021

Usage

JavaScript
nebula.getYoutubeID(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){
    var thisVideo = nebulaVideos[nebula.getYoutubeID(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 ID available. The fallback stack is Youtube ID, Debug ID, ?v parameter from the video URL or the iframe src, ID attribute of the iframe element itself.

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 537.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.getYoutubeID = function(target){
        let id;
    
        //If getVideoData is available in the API
        if ( target.getVideoData ){
            id = target.getVideoData().id || target.getVideoData().video_id;
        }
    
        //Make sure the ID was available within the getVideoData() otherwise use alternate methods
        if ( !id ){
            if ( target.getDebugText ){
                id = JSON.parse(target.getDebugText()).debug_videoId;
            } else if ( typeof target.getVideoUrl === 'function' ){
                id = nebula.get('v', target.getVideoUrl()); //Parse the video URL for the ID or use the iframe ID
            } else {
                id = jQuery(target.getIframe()).attr('src').split('?')[0].split('/').pop() || jQuery(target.getIframe()).attr('id'); //Parse the video URL for the ID or use the iframe ID
            }
        }
    
        return id;
    };
    

    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.getYoutubeID = 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.getYoutubeID = 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.getYoutubeID === 'function' ){
            nebula.getYoutubeID = function(target){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });