使用jQuery加载脚本 [英] Loading scripts using jQuery

查看:163
本文介绍了使用jQuery加载脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$.ajax({ url: "plugin.js", dataType: 'script', cache: true, success: function() {
    alert('loaded');
}});

1)我不能让脚本加载,可能是由于不正确的路径,但我如何确定正确的路径?上述code是在init.js,plugin.js也是在同一个文件夹中。

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)我可以加载多个插件在一次与相同的请求?例如。 plugin.js,anotherplugin.js?

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

推荐答案

您需要使用getScript加入脚本,而不是阿贾克斯。 Ajax是用于装载数据,而不是执行code。

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:

var scripts = ['plugin.js', 'test.js'];
for(var i = 0; i < scripts.length; i++) {
  $.getScript(scripts[i], function() {
    alert('script loaded');
  });
}

这篇关于使用jQuery加载脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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