Jquery .load 未加载 javascript 或 flash
我正在尝试使用 Jquery 将页面“/test/2”加载到主页上的 div 中。我设置了 2 个版本,一个使用 iframe,另一个使用 jQuery 加载函数。当我使用 ajax 加载功能时,页面的 youtube 部分(我使用 java 脚本加载)显示“您需要 Flash 播放器 8+ 并启用 JavaScript 才能观看此视频”。 ;然而。当我使用 iFrames 时,它可以完美加载。
这是我用来加载页面的代码(使用 jquery):
<前><代码><脚本> $(文档).ready(函数(){ $('#theList li a').click(function(){ $('#containerDiv').load($(this).attr('href')); 返回假; }); });
这是我用来在“/test/2”页面上加载 youtube 的代码,该代码直接来自 youtube 的 API 文档。
<div id="ytapiplayer">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
<script type="text/javascript">
// <![CDATA[
// allowScriptAccess must be set to allow the Javascript from one
// domain to access the swf on the youtube domain
var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };
// this sets the id of the object or embed tag to 'myytplayer'.
// You then use this id to access the swf and make calls to the player's API
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/{{video.video_id}}?border=0&autoplay=1;controls=0;hd=1;enablejsapi=1&playerapiid=ytplayer",
"ytapiplayer", "680", "380", "8", null, null, params, atts);
//]]>
</script>
奇怪的是,它可以与普通的 YouTube 嵌入和 iframe 完美配合,但不能与 jQuery 加载函数配合使用。我可以补充一点,我对 jQuery 比较陌生,所以我的加载函数可能存在我不知道的问题。
编辑:这似乎是导致问题的原因(即我收到 Uncaught ReferenceError:google 未定义)。在我尝试加载的文件中,我有:
<script src="//www.google.com/jsapi"></script>
<script> google.load("swfobject", "2.1"); </script>
创建 youtube 对象所需的。仍然不知道为什么它无法加载。
I am trying to load a page "/test/2" using Jquery into a div on the main page. I have 2 versions set up, one using iframes and the other using the jQuery load function. When I use the ajax load function the page's youtube portion (which I load using java-script) displays "You need Flash player 8+ and JavaScript enabled to view this video." ; however. when I use iFrames it loads perfectly.
Here is the code I'm using to load the page (using jquery):
<script> $(document).ready(function(){ $('#theList li a').click(function(){ $('#containerDiv').load($(this).attr('href')); return false; }); }); </script>
Here is the code I am using to load youtube on the page "/test/2" which is straight from youtube's API documentation.
<div id="ytapiplayer">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
<script type="text/javascript">
// <![CDATA[
// allowScriptAccess must be set to allow the Javascript from one
// domain to access the swf on the youtube domain
var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };
// this sets the id of the object or embed tag to 'myytplayer'.
// You then use this id to access the swf and make calls to the player's API
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/{{video.video_id}}?border=0&autoplay=1;controls=0;hd=1;enablejsapi=1&playerapiid=ytplayer",
"ytapiplayer", "680", "380", "8", null, null, params, atts);
//]]>
</script>
The weird thing is that it works perfectly with a normal youtube embed, and with iframes, but just not with a jQuery load function. Might I add that I am relatively new to jQuery, so there may be an issue with my load function that I am unaware of.
EDIT: This is what seems to cause the issue (i.e. I get an Uncaught ReferenceError: google is not defined). In the file I am trying to load, I have:
<script src="//www.google.com/jsapi"></script>
<script> google.load("swfobject", "2.1"); </script>
Which is needed to create the youtube object. Still unaware as to why it won't load.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery的加载函数将剥离脚本标签..
请参阅此相关SO问题 jquery load() 剥离脚本标签 - 解决方法?
jQuery's load function will strip script tags..
see this related SO question jquery load() strips script tags - workaround?
实现此目的的另一种方法是使用
get()
而不是load()
,并在success
回调中创建内容。在此示例中,服务器将仅返回视频 id,而不是 div 和脚本:Another way to accomplish this would be to use
get()
instead ofload()
, and to create the content in thesuccess
callback. In this example, the server would just return the video id, not the div and script: