1

I want to dynamically make different YouTube JSON API requests and then use a function to sort the data I want.

Currently following the examples on the YouTube data JSON API site here.

Here is my current code which is getting me the id's nicely. My question is currently it's using a script tag and the callback URL query to use my function. Is there a different way that I can implement in JavaScript only and keep doing requests that call my getid function?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
function getid(data) {
    var i=0;
    for(items in data.data){
        i++
         console.log(data.data.items[i].id)  
    }
}
</script><script type="text/javascript" src="https://gdata.youtube.com/feeds/api/videos?q=surfing&v=2&alt=jsonc&callback=getid"></script>
</body>
</html>

1 Answer 1

1

jsonp function below will dynamically create a script tag and append it to the body.

function jsonp(url,callback) {                    
  var script = document.createElement("script");        
  script.setAttribute("src",url);
  script.setAttribute("type","text/javascript");                
  document.body.appendChild(script);
}

function getid(data) {
  var i=0;

  for(items in data.data){
    i++
    console.log(data.data.items[i].id)  
  }
}

// pass a url and callback
var url = "https://gdata.youtube.com/feeds/api/videos?q=surfing&v=2&alt=jsonc&callback=getid";  
jsonp(url, getid)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.