this may sound a bit noobish, but I'm trying to retrieve the video information for a YouTube video (I saw in some tutorial) basically here's the code
function youtubeFeedCallback1(data) {
var s = '';
var k = '';
s += data.entry.title.$t;
k += data.entry.media$group.media$thumbnail[2].url;
vidtitle1=s
vidthumb1=k
}
<script type="text/javascript" id="javaone" src='/'+vidid[0]+'?v=2&alt=json-in-script&callback=youtubeFeedCallback1' ></script>
As you can see, I'm trying to insert the var "vidid[0]" in the src, which doesnt work. Now, I did do my homework, but when i set a new script attribute and set the new src tot that it still does not work. Can anyone help me here?
this may sound a bit noobish, but I'm trying to retrieve the video information for a YouTube video (I saw in some tutorial) basically here's the code
function youtubeFeedCallback1(data) {
var s = '';
var k = '';
s += data.entry.title.$t;
k += data.entry.media$group.media$thumbnail[2].url;
vidtitle1=s
vidthumb1=k
}
<script type="text/javascript" id="javaone" src='http://gdata.youtube./feeds/api/videos/'+vidid[0]+'?v=2&alt=json-in-script&callback=youtubeFeedCallback1' ></script>
As you can see, I'm trying to insert the var "vidid[0]" in the src, which doesnt work. Now, I did do my homework, but when i set a new script attribute and set the new src tot that it still does not work. Can anyone help me here?
Share Improve this question asked Jan 28, 2013 at 3:10 B''H Bi'ezras -- Boruch HashemB''H Bi'ezras -- Boruch Hashem 1 2- 1 What does the function have to do with the script element? – David G Commented Jan 28, 2013 at 3:13
- it takes the "callback=youtubeFeedCallback1" function from the URL and usees it to get the title & thumb of the youtube video – B''H Bi'ezras -- Boruch Hashem Commented Jan 28, 2013 at 3:17
5 Answers
Reset to default 6What you're trying to do is called called JSONP. Since you can't use a regular Ajax call to fetch JSON from another domain (it would have security implications), you have to add a script that will call the callback function you specify, and pass it the JSON. As other answers say, you have to create the script tag programmatically. I wouldn't use document.write
for that, because it won't work after page load (the new script would replace the whole document). But there is a very simple alternative:
function youtubeFeedCallback1(data) {
var s = '';
var k = '';
s += data.entry.title.$t;
k += data.entry.media$group.media$thumbnail[2].url;
vidtitle1=s;
vidthumb1=k;
}
var script = document.createElement('script');
script.src = "http://gdata.youtube./feeds/api/videos/" + vidid[0] + "?v=2&alt=json-in-script&callback=youtubeFeedCallback1";
document.body.appendChild(script);
One last remendation: I see you have global variables on your callback, vidtitle1
and vidthumb1
. Whatever you need to do with their values, do it from the callback (and preferably get rid of the global variables), or chances are it won't work. The data will be loaded asynchronously, so the variables are only guaranteed to contain their values after the callback runs.
Though CDATA works fine, using document.createElement is also a great choice.. Especially if you intend to append some value to a URL, say for cache busting..
<script type="text/javascript">
var versionJSLink = "/Folder/sub_folder/version.js?version=" + Math.random();
JSElement = document.createElement('script');
JSElement.src = versionJSLink;
JSElement.onload = OnceLoaded;
document.getElementsByTagName('head')[0].appendChild(JSElement);
function OnceLoaded() {
// Once loaded.. load other JS or CSS or call objects of version.js
}
</script>
Have fun.. :)
Either document.write it or createElement()/appendChild()
var id = "asdf";
document.write('\x3Cscript type="text/javascript" src="foo.js?id=' + id + '">\x3C/script>');
Where is the code snippet within the page? If it's inside a <script>
tag, the problem is that the script tag is being interpreted as javascript, not html. Instead, from the original script tag you can use document.write
to write the script tag to the dom.
The new code would be something like:
function youtubeFeedCallback1(data) {
var s = '';
var k = '';
s += data.entry.title.$t;
k += data.entry.media$group.media$thumbnail[2].url;
vidtitle1=s
vidthumb1=k
}
document.write("<script type=\"text/javascript\" id=\"javaone\" src=\"http://gdata.youtube./feeds/api/videos/\"+vidid[0]+\"?v=2&alt=json-in-script&callback=youtubeFeedCallback1\"></script>");
The script tag needs to be written in javascript to access the vidid
variable, but that means you have to manipulate the DOM in order to add the script tag since you're no longer in HTML.
function youtubeFeedCallback1(data) {
var s = '';
var k = '';
s += data.entry.title.$t;
k += data.entry.media$group.media$thumbnail[2].url;
vidtitle1 = s
vidthumb1 = k
}
<script id="javaone"></script>
<script>
var fullSrc = "http://gdata.youtube./feeds/api/videos/"+vidid[0]+"?v=2&alt=json-in-script&callback=youtubeFeedCallback1";
$('#javaone').attr("src",fullSrc);
</script>
<script src="https://ajax.googleapis./ajax/libs/jquery/1.10.1/jquery.min.js"></script>
I think you get the idea: 1. create empty script container. 2. define the plete link in a variable. 3. set this variable as a src attribute to the empty script.
p.s. don't know if it works in js. This is jquery