重复 Javascript 注入以进行 YouTube API 调用?
我正在创建一个利用 YouTube API 的网页。我希望能够在每次用户执行某些操作时对 YouTube API 发起 AJAX 样式调用,并使用调用结果更新页内元素。 YouTube 的文档建议使用 Javascript 注入来解决从我的网页对外部域 (gdata.youtube.com) 进行 AJAX 调用时涉及的跨域权限问题。例如:
<script
type="text/javascript"
src="http://gdata.youtube.com/feeds/users/GoogleDevelopers/uploads?alt=json-in-script&format=5&callback=showMyVideos">
</script>
放置在 HTML BODY 内的此代码段使用“callback”参数从“GoogleDevelopers”通道发出视频提要请求,从而在调用返回时调用 showMyVideos()。 showMyVideos()(此处未显示且任意命名)使用 DOM 调用来使用调用结果更新命名的 DIV。
我想要执行相同的操作,除了动态而不是像上面所示的静态操作。例如,我见过这样的代码:
var s = document.createElement('script');
s.type = "text/javascript";
s.src = "http://gdata.youtube.com/feeds/users/GoogleDevelopers/uploads?alt=json-in-script&format=5&callback=showMyVideos"
var head = document.getElementsByTagName('head')[0];
head.appendChild(s);
我担心的是,如果我使用这种方法,由于我将进行许多 AJAX 调用,我将在网页 DOM 树中积累越来越多动态生成的 Javascript DOM 元素,我担心什么可能对浏览器或其他任何东西造成的后果。
有没有一种方法可以以这种方式进行 Javascript 注入,而不会使 HEAD 元素中的大量 Javascript 元素变得混乱?简洁的代码示例将不胜感激。
——罗施勒
I am creating a web page that utilizes the YouTube API. I want to be able to fire off an AJAX style call to the YouTube API every time the user takes certain actions and update in-page elements with the results of the calls. YouTube's documentation recommends using Javascript injection to get around the cross domain permission issues that would be involved with making AJAX calls to a foreign domain (gdata.youtube.com) from my web page. For example:
<script
type="text/javascript"
src="http://gdata.youtube.com/feeds/users/GoogleDevelopers/uploads?alt=json-in-script&format=5&callback=showMyVideos">
</script>
This snippet placed inside the HTML BODY makes a video feed request from the "GoogleDevelopers" channel with the "callback" parameter causing a call to showMyVideos() when the call returns. showMyVideos(), not shown here and arbitrarily named, uses DOM calls to update a named DIV with the results of the call.
I want to do the same operation except dynamically instead of statically like shown above. For example, I have seen code like this:
var s = document.createElement('script');
s.type = "text/javascript";
s.src = "http://gdata.youtube.com/feeds/users/GoogleDevelopers/uploads?alt=json-in-script&format=5&callback=showMyVideos"
var head = document.getElementsByTagName('head')[0];
head.appendChild(s);
My concern is if I use this approach, since I will be making many AJAX calls, I will accumulate more and more dynamically generated Javascript DOM elements in the web page DOM tree, and I worry what consequences that might have to the browser or anything else.
Is there a way to do Javascript injection in this style without cluttering up the HEAD element lots of Javascript elements? A concise code sample would be appreciated.
-- roschler
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,没那么难,在获取数据后,您可以通过删除脚本元素来清理数据。
Well, not that hard, after you've gotten your data you clean up by removing the script element.