JavaScript 自动加载器?

发布于 2024-11-17 01:04:34 字数 305 浏览 2 评论 0原文

有没有一种解决方案可以让 JavaScript/jQuery 在需要时自动加载依赖文件?例如,考虑以下场景:

  1. 我有一个自动加载器脚本,用于侦听何时需要加载特定脚本。
  2. 调用 jQuery dialog() 插件。
  3. 自动加载器被告知监听此插件何时被调用,并加载 jQuery UI。
  4. 如果将来调用更多对话框,则不会加载所需的脚本。

对于仅仅尝试限制带宽来说,这是否太过费力?我是否应该将所有核心文件包含在一个超级包中并完成它?

谢谢您的宝贵时间。

Is there a solution out there where I can have JavaScript/jQuery autoload dependent files when needed? For example, consider this scenario:

  1. I have an autoloader script listening for when a particular script needs to be loaded.
  2. A jQuery dialog() plugin is called.
  3. The autoloader is told to listen for when this plugin is called, and loads the jQuery UI.
  4. If more dialogs are called in the future, the required script will not be loaded.

Is this too much effort for simply trying to limit bandwidth? Should I just include all of the core files in one superpackage and be done with it?

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

淡淡離愁欲言轉身 2024-11-24 01:04:34

是的,您应该将所有脚本包含在一个文件中。或者至少它们中的大多数按如下方式分组:jquery.js、global.js(这是经常使用的脚本 - 在不止一个、两个页面上)和 page_specyfic.js。
想象一下,调用 dialog() 并且用户必须等待 .js 下载和插件初始化。
节省带宽(如果有的话)不值得损害用户体验。

Yes you should inclde all of the scripts in one file. Or at least most of them groupped like this: jquery.js, global.js (that's where frequently - on more than one, two pages - used scripts should be) and page_specyfic.js.
Imagine that a dialog() is called and the user has to wait for .js to download and plugins to initialise.
Savings in bandwith (if any) wouldn't be worth harming the users expirience.

四叶草在未来唯美盛开 2024-11-24 01:04:34

有许多按需脚本加载的示例。例如,remy Sharp 在他的博客上有一个代码示例您可以按原样使用它,也可以将其转换为 jQuery 插件。不幸的是,它可能不适用于所有浏览器。

还有jQuery Lazy Plugin Loader,它按需加载 jQuery 插件,而不是预先加载。要使用它,您需要为您正在使用的每个 jQuery UI 部分设置延迟加载,如下所示(名称将是您使用的每个部分的函数名称):

$.lazy([{
   src: 'jquery-ui-1.8.14.custom.min.js',
   name: 'dialog'
}]);

您还可以使用 这个关于按需加载 jQuery 本身的问题。例如,您可以在需要加载 jQuery UI 时动态创建脚本标记。

最后,既然您正在谈论 jQuery UI,请考虑从 Google 的 CDN 获取它,无论如何它都可能缓存在用户的浏览器中。

There are many examples of on demand script loading out there. For example remy sharp has a code sample on his blog that you could either use as is or turn into a jQuery plugin. Unfortunately it may not work in all browsers.

There is also the jQuery Lazy Plugin Loader which loads jQuery plugins on demand rather than up-front. To use it you would need to set up lazy loading for each piece of jQuery UI you are using as follows (name will be the function name for each piece you use):

$.lazy([{
   src: 'jquery-ui-1.8.14.custom.min.js',
   name: 'dialog'
}]);

You can also use the techniques in this question about loading jQuery itself on demand. For example you can dynamically create a script tag at the time needed to load jQuery UI.

Finally since you are talking about jQuery UI consider getting it from Google's CDN, which is likely cached in the user's browser anyway.

梦境 2024-11-24 01:04:34

你可以尝试这个新的 jquery 插件。与 yeapnope.js 类似,但更有意义。

http://plugins.jquery.com/plugin-tags/autoloader

$(document).autoLoader(
    {
      test: $.ui,
      loadScript: "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-   ui.min.js",
      complete: function(){
         console.log($.ui);
      }
    }
);

You can try this new jquery plugin. Works like yeapnope.js but more make sense.

http://plugins.jquery.com/plugin-tags/autoloader

$(document).autoLoader(
    {
      test: $.ui,
      loadScript: "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-   ui.min.js",
      complete: function(){
         console.log($.ui);
      }
    }
);
鲜肉鲜肉永远不皱 2024-11-24 01:04:34

我不会太担心。文件被缓存。一旦站点中的一个页面加载了 jquery UI(或任何其他包含文件,如 CSS),下次需要时它将位于用户的浏览器缓存中,数天/数周内不会再次加载

I wouldn't worry too much. The files are cached. Once one page in your site loads the jquery UI (or any other include file like CSS), the next time it's needed it will be in the user's browser cache, never to be loaded again for days/weeks

前事休说 2024-11-24 01:04:34

听起来你想要一个脚本加载器。

不过,通常无法跨浏览器同步加载脚本,因此脚本加载器必然是异步的。您所要求的并不完全可能,因为脚本需要加载、调用回调,然后继续。您不想调用某个插件而不知道它是否同步执行,这会让您陷入问题的世界。

我建议你看看 DeferJS,一个 jQuery 的脚本加载器:
https://github.com/BorisMoore/jsdefer

Sounds like you want a script loader.

You can't generally do synchronous loading of scripts across browsers, though, so script loaders are necessarily asynchronous. What you're asking for isn't exactly possible since the script needs to load, call a callback, and then continue. You wouldn't want to call some plugin and not know whether it is executing synchronously or not, that gets you into a world of problems.

I recommend you look at DeferJS, a script loader for jQuery:
https://github.com/BorisMoore/jsdefer

不忘初心 2024-11-24 01:04:34

从您的评论来看,您的部分愿望似乎是保持代码井井有条。我会推荐 RequireJs。它可以让您将代码分解为具有显式依赖关系的清晰分离的模块。然后,当您想要投入生产时,有一个构建工具可以将它们全部合并到您想要服务的(请求/带宽节省)2-3 个文件中。

From your comments, part of your wish seems to be to keep your code organized. I would recommend RequireJs. It lets you break your code up into clearly separated modules with explicit dependencies. Then when you want to go to production, there's a build tool that will merge them all back together into the (request/bandwidth saving) 2-3 files you want to serve.

愁以何悠 2024-11-24 01:04:34

是的,我也想过实施这样的事情。我不确定它最终是否值得,但有很多库可以为您执行此操作,例如 ensure

Yeah, I have also thought about implementing something like this. I am not sure if it would be worthwhile or not in the end but there are quite a few libraries to do this for you like ensure

一身骄傲 2024-11-24 01:04:34

你可以尝试这样的事情,但这会很痛苦。基本上,您正在检查捕获的错误类型,并在对话框(您尝试调用的函数不存在)加载该函数并尝试再次调用该方法时发出消息。就像我说的,除非想到一些优雅的解决方案,否则在任何地方都这样做会很痛苦。

function loadDialog() {
    $('#myDialog').dialog({});
}

try { 
    loadDialog()
} catch(e) {
    if (e && e.type && e.type=='not_defined' && e.message == 'dialog is not defined') {
        //load jQuery plugins...
        loadDialog();
    }
}

you could try something like this but it would be a pain. basically you are checking the type of error caught and message if dialog (the function you are trying to call doesn't exist) load the function and try calling the method again. Like I said it would be a pain to do this everywhere unless some elegant solution was thought of.

function loadDialog() {
    $('#myDialog').dialog({});
}

try { 
    loadDialog()
} catch(e) {
    if (e && e.type && e.type=='not_defined' && e.message == 'dialog is not defined') {
        //load jQuery plugins...
        loadDialog();
    }
}
呆头 2024-11-24 01:04:34

这是上述评论的后续帖子:

<link rel="stylesheet" href="../system/stylesheets/universal.css" />
<link rel="stylesheet" href="../system/stylesheets/jquery-ui.min.css" />
<link rel="stylesheet" href="../system/stylesheets/uploadify.css" />
<link rel="stylesheet" href="system/stylesheets/style.css" />
<script src="../system/javascripts/swfobject.js"></script>

<script src="../system/javascripts/jquery.min.js"></script>
<script src="../system/javascripts/jquery-ui.min.js"></script>
<script src="../system/javascripts/global.jquery.js"></script>
<script src="../system/javascripts/analog.jquery.js"></script>
<script src="../system/javascripts/funtip.jquery.js"></script>
<script src="../system/javascripts/uploadify.jquery.js"></script>
<script src="system/javascripts/install.jquery.js"></script>
<link rel="stylesheet" href="system/templates/stylesheets/style.css" />
<script>
$(document).ready(function() {
    $(':text, :password, textarea').funtip();
});
</script>

This is a follow-up post for a comment above:

<link rel="stylesheet" href="../system/stylesheets/universal.css" />
<link rel="stylesheet" href="../system/stylesheets/jquery-ui.min.css" />
<link rel="stylesheet" href="../system/stylesheets/uploadify.css" />
<link rel="stylesheet" href="system/stylesheets/style.css" />
<script src="../system/javascripts/swfobject.js"></script>

<script src="../system/javascripts/jquery.min.js"></script>
<script src="../system/javascripts/jquery-ui.min.js"></script>
<script src="../system/javascripts/global.jquery.js"></script>
<script src="../system/javascripts/analog.jquery.js"></script>
<script src="../system/javascripts/funtip.jquery.js"></script>
<script src="../system/javascripts/uploadify.jquery.js"></script>
<script src="system/javascripts/install.jquery.js"></script>
<link rel="stylesheet" href="system/templates/stylesheets/style.css" />
<script>
$(document).ready(function() {
    $(':text, :password, textarea').funtip();
});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文