how to check the valid Youtube url using jquery

后端 未结 8 746
心在旅途
心在旅途 2020-12-10 14:02

In Jquery i want to check the specific url from youtube alone and show success status and others i want to skip by stating it as not valid url

var _videoUrl          


        
8条回答
  •  自闭症患者
    2020-12-10 14:38

    I will give some extra :

    For both Youtube and Vimeo to checking those IDs are really vaild or not

    var GoogleKey = 'XXXXXXXXXX';
    function GetVideoById(id) {
        var site = !isNaN(id) ? 'vimeo' : 'youtube';
        var data = false;
        if(site==='youtube') {
            $.ajax({
                async: false,
                url: 'https://www.googleapis.com/youtube/v3/videos?id='+id+'&key='+GoogleKey+'&part=snippet',
                success: function(r) {
                    if(r['items'].length) {
                        data = {'type':'youtube','data':r['items'][0]['snippet']};
                    }
                }
            });
        } else {
            $.ajax({
                async: false,
                url: 'http://vimeo.com/api/v2/video/'+id+'.json',
                success: function(r) {
                    data = {'type':'vimeo','data':r[0]};
                }
            });
        }
        return data;
    }
    

    So Example runs :

    if(GetVideoById('YykjpeuMNEk')) {
       // youtube + data
    }
    
    if(GetVideoById('162334918')) {
       // vimeo + data
    }
    if(GetVideoById('999999')) {
       // nothing (because false)
    }
    if(GetVideoById('abcdefg')) {
       // nothing (because false)
    }
    

提交回复
热议问题