I'm studying very closely a YQL query example. There is a html script call to an API url address with callback function identified.
If I include the callback as a separate <script></script>
things work fine. It totally fails if the code is contained in a single <script></script>
tag.
This works:
<script>
function top_stories(o) {
var items = o.query.results.item;
var output = '';
var no_items = items.length;
for (var i = 0; i < no_items; i++) {
var title = items[i].title;
var link = items[i].link;
var desc = items[i].description;
output += "<h3><a href='" + link + "'>" + title + "</a></h3>" + desc + "<hr/>";
}
// Place news stories in div tag
document.getElementById('results').innerHTML = output;
}
</script>
<script src='https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D%22http%3A%2F%2Frss.news.yahoo.com%2Frss%2Ftopstories%22&format=json&callback=top_stories'></script>
While this next bit fails to function at all.
<script>
function top_stories(o) {
var items = o.query.results.item;
(... same as above)
document.getElementById('results').innerHTML = output;
}
src='https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D%22http%3A%2F%2Frss.news.yahoo.com%2Frss%2Ftopstories%22&format=json&callback=top_stories';
</script>
Non functional jsfiddle: https://jsfiddle.net/ogcoaaff/
Swap a few comments and success: https://jsfiddle.net/ogcoaaff/1/
I don't believe this is a timing / loading sequence issue. No way. I was pretty careful to push the scripts into the jsfiddle HTML box and not the script box. I did not want to use jQuery and have that cloud the discussion.
Can someone explain what's going on here? What am I missing? (Note: I was hoping to run a quick call to a YQL api from totally within a javascript file in another application where I don't have access to <script>
calls. (and where I can't use $.JSON and prefer not to use XMLHttpRequest().)
I'd just really would like to understand what is happening here. Anybody know the details (or better yet, a tech reference explaining this behavior?)
Many thanks.