jQuery加载似乎未处理< script type =".." src ="a.js">? [英] JQuery load doesn't seem to process <script type=".." src="a.js">?

查看:81
本文介绍了jQuery加载似乎未处理< script type =".." src ="a.js">?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载这样的页面:

I'm loading a page like this:

 function loadGUIComponents() {
// load contents into each accordion area
$("#listEntryDiv").load("listEntry/listEntryIndex.html");
$("#openEntryDiv").load("openEntry/openEntryIndex.html");
 };

在listEntryIndex.html中,可以这样调用脚本:

In listEntryIndex.html, scripts like this can be called right:

<script>
alert("AHA!");
</script>
<h1>the world is helloed.</h1>

但是,如果我尝试将JS外部化为单独的文件:

But if I try to externalize JS into a separate file:

<script type="text/javascript" src="listEntryIndex.js"></script>
<h1>the world is helloed.</h1>

该脚本将永远不会被调用.我确实在Google上搜索了很多,但没有找到原因.人们问为什么加载后不调用其javascript,但是我的原因是为什么加载后仅不调用标记.谢谢!

The script will never be called. I really have googled a lot and am not getting the reason. People are asking why their javascripts are not called after being loaded, but mine is why only the tag is not called after being loaded. Thanks!

推荐答案

不是问题,不是jQuery load,从根本上说是有效的:

It's not jQuery load that's the problem, fundamentally that works: Live example That example is using this code:

$("#target").load("http://jsbin.com/oqefe4");

要加载此文件:

<p>Test</p>
<script src='http://jsbin.com/ewexi4'></script>

...引用了这一点的

...which references this one:

alert("Hello");

(我在上面使用了完整路径,但是这里是带有相对路径的版本;可以也可以.)

(I used full paths in the above, but here's a version with relative paths; that works fine as well.)

这表明实际问题出在其他地方.您必须逐步调试,以查看发生了什么.幸运的是,如今,几乎每一个浏览器都内置是那些没有的免费选项.

This suggests that the actual problem lies elsewhere. You'll have to step through with a debugger to see what's going on. Fortunately, just about every browser has one built in these days and there are free options for those that don't.

更新:相对路径可能存在问题.将HTML文件中的文本加载到当前文档中后,该HTML文本中script(和img等)标签内的路径将相对于当前文档的位置进行解析,而不是您要加载的文档的位置.我会在调试器中查找404错误,这可能是路径问题.这个想法是由@Darth ___(我认为还有更多)对问题的评论已删除引发的,该评论质疑了路径...( Edit :是DarthJDG;他现在将其发布为答案.我敢打赌,这是正确的.)

Update: You might have an issue with relative paths. When the text from your HTML file is loaded into the current document, paths within script (and img, etc.) tags in that HTML text will be resolved relative to the current document's location, not the location of the document you're loading. I'd look for 404 errors in the debugger, it's probably a path problem. This thought was triggered by a now-deleted comment on the question by @Darth___ (I think there was more to it) which questioned the paths... (Edit: It was DarthJDG; he's now posted that as an answer. I bet it's the right one.)

具体来说,说您的主要文档在

So specifically, say your main document is at

http://example.com/foo/maindocument.html

您的load呼叫正在加载listEntry/listEntryIndex.htmlopenEntry/openEntryIndex.html,它们将是:

Your load call is loading listEntry/listEntryIndex.html and openEntry/openEntryIndex.html, and those will be:

http://example.com/foo/listEntry/listEntryIndex.html
http://example.com/foo/openEntry/openEntryIndex.html

如果其中的HTML 具有您列出的脚本标签listEntryIndex.js,则将相对于maindocument.html解析,相对于listEntryIndex.html解析,因此:

If the HTML within those has the script tag you listed, listEntryIndex.js, it will be resolved relative to maindocument.html, not relative to listEntryIndex.html, and thus:

http://example.com/foo/listEntryIndex.js

不是

http://example.com/foo/listEntry/listEntryIndex.js

这篇关于jQuery加载似乎未处理&lt; script type =".." src ="a.js">?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆