4
<script type="text/javascript">

        $(document).ready(function() {

            var chanName = "";

            loadchannelID("Pewdiepie");

            function loadchannelID(name){

                chanName = name;

                var nameid= 'https://www.googleapis.com/youtube/v3/channels?part=id&forUsername='+name+'&key=AIzaSyCppVQFcUiLE8-Z2JSyjpvvek8WfPeCfcI'
                $.getJSON(nameid, function(data) {
                    $('#ytID').html(data.items[0].id); 
                //MAKE IT TO A VAR                    
            });


            }

            function loadChannel(data) {

                var url = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&id='+id+'&key=AIzaSyCppVQFcUiLE8-Z2JSyjpvvek8WfPeCfcI';
                $.getJSON(url, function(data) {
                    $('#odometer').html(data.items[0].statistics.subscriberCount);
                    $('#viewCount').html(data.items[0].statistics.viewCount);
                    $('#commentCount').html(data.items[0].statistics.commentCount);
                    $('#videoCount').html(data.items[0].statistics.videoCount);
                });

                var url1 = 'https://www.googleapis.com/youtube/v3/channels?part=snippet&id='+id+'&key=AIzaSyCppVQFcUiLE8-Z2JSyjpvvek8WfPeCfcI';
                $.getJSON(url1, function(data){
                    $('#ytName').html(data.items[0].snippet.title);
                    $('#ytDis').html(data.items[0].snippet.description);
                    $('#ytImage').html('<a href=\"https://www.youtube.com/'+ data.items[0].snippet.customUrl + '\"> <img class="img-circle" src=\"'+data.items[0].snippet.thumbnails.medium.url+'\" ></a>');
                });
            }

            setInterval( function() {
                var url = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&id='+chanName+'&key=AIzaSyCppVQFcUiLE8-Z2JSyjpvvek8WfPeCfcI';
                $.getJSON(url, function(data) {
                    $('#odometer').html(data.items[0].statistics.subscriberCount);
                    $('#viewCount').html(data.items[0].statistics.viewCount);
                    $('#commentCount').html(data.items[0].statistics.commentCount);
                    $('#videoCount').html(data.items[0].statistics.videoCount);
                });

            }, 5000);

            $('#update').click( function(){
                loadchannelID($('#chnlName').val());
            })            
        });

    </script>

This is what is have done so far. I need to get the id form a Youtube channel but i have a Youtube name. So i need to convert the name to the youtube channel id. The "Function loadchannelID" is what i have so far, it works but i need to get the #ytID to a var. But I dont know how to do that. The other function is to show the data from the Channel ID and that will work aswell if the id is converted to a var. Please help! Thanks!

Dani Kemper
  • 343
  • 2
  • 10

4 Answers4

1

I hope this is what you want to achieve:

var chanName = "";
var chanID = 0;

loadchannelID("Pewdiepie");

function loadchannelID(name){
  chanName = name;
  var nameid= 'https://www.googleapis.com/youtube/v3/channels?part=id&forUsername='+name+'&key=AIzaSyCppVQFcUiLE8-Z2JSyjpvvek8WfPeCfcI'
  $.getJSON(nameid, function(data) {
    chanID = data.items[0].id;
    $('#ytID').html(chanID); 
    loadChannel(chanID); // now, you know the ID, pass it to "loadChannel"            
  });
}

function loadChannel (id) {
  var url = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&id='+id+'&key=AIzaSyCppVQFcUiLE8-Z2JSyjpvvek8WfPeCfcI';
  $.getJSON(url, function(data) {
    $('#odometer').html(data.items[0].statistics.subscriberCount);
    $('#viewCount').html(data.items[0].statistics.viewCount);
    $('#commentCount').html(data.items[0].statistics.commentCount);
    $('#videoCount').html(data.items[0].statistics.videoCount);
  });

  var url1 = 'https://www.googleapis.com/youtube/v3/channels?part=snippet&id='+id+'&key=AIzaSyCppVQFcUiLE8-Z2JSyjpvvek8WfPeCfcI';
  $.getJSON(url1, function(data){
    $('#ytName').html(data.items[0].snippet.title);
    $('#ytDis').html(data.items[0].snippet.description);
    $('#ytImage').html('<a href=\"https://www.youtube.com/'+ data.items[0].snippet.customUrl + '\"> <img class="img-circle" src=\"'+data.items[0].snippet.thumbnails.medium.url+'\" ></a>');
  });
}
PeterMader
  • 6,987
  • 1
  • 21
  • 31
0

I am not clear if you are trying to get the value or you try to make the url but in both cases you can do:

if you want to get the param value you can use:

var url_string = "https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=Pewdiepie&key=AIzaSyCppVQFcUiLE8-Z2JSyjpvvek8WfPeCfcI";
var url = new URL(url_string);
var forUsername = url.searchParams.get("forUsername");

if you want to set the param you can do:

var forUsername = url.searchParams.set("forUsername", yourValueHere);
Ivan Lynch
  • 563
  • 5
  • 23
0

As ive already said, your code is async, so you may use Promises like this:

function loadchannelID(name){
 return new Promise(function(resolve){
    var chanName = name;
    var nameid= 'https://www.googleapis.com/youtube/v3/channels?part=id&forUsername='+name+'&key=AIzaSyCppVQFcUiLE8-Z2JSyjpvvek8WfPeCfcI'
  $.getJSON(nameid, function(data) {
     $('#ytID').html(data.items[0].id); 
     //lets resolve the promise
     resolve(data.items[0].id);                
  });
 });
}

So you can use it like this:

loadChanelID("test").then(function(name){
 alert("name is"+name);
});

If you change

function loadChannel(data) {

To:

function loadChannel(id){

You can do:

loadChanelID("test").then(loadChannel);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
-1

I assume you want to use your var within the class and chanName is your channel ID and name container array

(function($, jsYouTube){

    var chanName = "";

    $.getChannelID = function loadchannelID(name){

        chanName = name;

        var nameid= 'https://www.googleapis.com/youtube/v3/channels?part=id&forUsername='+name+'&key=AIzaSyCppVQFcUiLE8-Z2JSyjpvvek8WfPeCfcI'
        $.getJSON(nameid, function(data) {
            $this.chanName['id'] = data.items[0].id ;
        });


    }
})(jQuery, 'jsYouTube');
Mason.Chase
  • 897
  • 1
  • 10
  • 21