使用 jQuery 加载脚本
$.ajax({ url: "plugin.js", dataType: 'script', cache: true, success: function() {
alert('loaded');
}});
1)我无法加载脚本,可能是由于路径不正确,但如何确定正确的路径?上面的代码在init.js中,plugin.js也在同一个文件夹中。
2)我可以使用同一请求一次加载多个插件吗?例如。插件.js,另一个插件.js?
root
|
|_ html > page.html
|
|_ static > js > init.js, plugin.js
感谢您的帮助
$.ajax({ url: "plugin.js", dataType: 'script', cache: true, success: function() {
alert('loaded');
}});
1) I can't get the script to load, probably due to incorrect path, but how do I determine the correct path? The above code is in init.js, plugin.js is also in the same folder.
2) Can I load multiple plugins at once with the same request? eg. plugin.js, anotherplugin.js?
root
|
|_ html > page.html
|
|_ static > js > init.js, plugin.js
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要使用 getScript,而不是 ajax。 Ajax 用于加载数据,而不是用于执行代码。
如果您需要加载多个文件,请尝试以下操作:
You need to use getScript, not ajax. Ajax is for loading data, not for executing code.
If you need to load multiple files, try something like this:
1) 该路径将相对于它加载的页面(而不是初始化脚本的路径),因为这是浏览器执行 ajax 请求时所在的 URL。
编辑:根据您的编辑,加载脚本的路径是
/static/js/plugin.js
(如果它将部署在您的根目录下)域),或../static/js/plugin.js
为了安全起见(假设将从中加载的所有页面都位于/html
中)。2) 不。如果它们位于不同的文件中,则它们需要是不同的请求。不过,您可以将它们合并到服务器端的一个文件中......
1) The path will be relative to the page it's loaded from (not the path of your init script) since that's the url the browser will be at when it executes the ajax request.
Edit: Based on your edit, the path to load your script from is either
/static/js/plugin.js
(if it'll be deployed at the root of your domain), or../static/js/plugin.js
to be safe (assuming all pages that it'll be loaded from will be in/html
).2) No. If they're in different files, they'll need to be different requests. You could merge them into one file on the server-side though...
作为更新,使用 jQuery 1.9.x 执行此操作的更好方法是使用 Deferreds-methods(即 $.when),如下所示:
.done()
回调函数有许多有用的参数。阅读文档: http://api.jquery.com/jQuery.when/
As an update, the better way to do this with jQuery 1.9.x is to use Deferreds-methods (i.e. $.when), such as follows:
The
.done()
callback function has a number of useful params.Read the documentation: http://api.jquery.com/jQuery.when/
查看 jQuery .getScript 函数,它将加载脚本并将其包含在当前脚本中文档。
1) 路径应该是
/static/js/plugin.js
,相对于您的文档。2) 否。每个文件均由单个 HTTP 请求加载。
Check out the jQuery .getScript function, which will load the script and include it in the current document.
1) The path should be
/static/js/plugin.js
, relative to your document.2) No. Each file is loaded by a single HTTP request.
我会检查 Firebug 的 net 选项卡,看看它是否已正确加载,以及它尝试加载的路径。
该路径来自包含 JavaScript 的文档。
我还保留一个像这样的配置对象(本例中为 PHP)
然后你可以这样做
I would check Firebug's net tab to see if it was loaded correctly, and the path it is trying to load from.
The path will be from the document where the JavaScript was included.
I also keep a config object like this (PHP in this example)
Then you could do
您可以使用
'../'
将脚本路径设置为基本路径。然后添加相对路径You can use
'../'
to set the script path as base path. Then add the relative path for the