将javascript变量添加到javascript src? [英] Add javascript variable to javascript src?
问题描述
这可能听起来有点不好,但我试图检索YouTube视频的视频信息(我在一些教程中看到)基本上是代码
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
}
正如您所看到的,我试图将varvidid [0]插入到src,这不起作用。现在,我做了我的功课,但是当我设置一个新的脚本属性并设置新的src tot时,它仍然不起作用。任何人都可以帮助我吗?
en.wikipedia.org/wiki/JSONPrel =nofollow> JSONP 。由于您不能使用常规的Ajax调用从另一个域中获取JSON(这会产生安全隐患),因此您必须添加一个脚本来调用您指定的回调函数,并将其传递给JSON。正如其他答案所说,您必须以编程方式创建脚本标记。我不会使用 document.write
,因为在加载页面后(新脚本会替换整个文档),它不起作用。但是有一个非常简单的选择:
$ b $ pre $ 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.com/feeds/api/videos/+ vidid [0] +?v = 2& amp; alt = json-in-script& amp; amp; amp; amp; amp; amp; amp; amp; amp;回调= youtubeFeedCallback1\" ;
document.body.appendChild(script);
最后一项建议:我看到您的回调中有全局变量 vidtitle1
和 vidthumb1
。无论您需要如何处理它们的值,都可以从回调中完成(最好是摆脱全局变量),否则很可能无法工作。数据将被异步加载,所以变量只能保证在回调运行后包含它们的值。
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.com/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?
What 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.com/feeds/api/videos/" + vidid[0] + "?v=2&alt=json-in-script&callback=youtubeFeedCallback1";
document.body.appendChild(script);
One last recommendation: 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.
这篇关于将javascript变量添加到javascript src?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!