JavaScript 自动加载器?
有没有一种解决方案可以让 JavaScript/jQuery 在需要时自动加载依赖文件?例如,考虑以下场景:
- 我有一个自动加载器脚本,用于侦听何时需要加载特定脚本。
- 调用 jQuery
dialog()
插件。 - 自动加载器被告知监听此插件何时被调用,并加载 jQuery UI。
- 如果将来调用更多对话框,则不会加载所需的脚本。
对于仅仅尝试限制带宽来说,这是否太过费力?我是否应该将所有核心文件包含在一个超级包中并完成它?
谢谢您的宝贵时间。
Is there a solution out there where I can have JavaScript/jQuery autoload dependent files when needed? For example, consider this scenario:
- I have an autoloader script listening for when a particular script needs to be loaded.
- A jQuery
dialog()
plugin is called. - The autoloader is told to listen for when this plugin is called, and loads the jQuery UI.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
是的,您应该将所有脚本包含在一个文件中。或者至少它们中的大多数按如下方式分组: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.
有许多按需脚本加载的示例。例如,remy Sharp 在他的博客上有一个代码示例您可以按原样使用它,也可以将其转换为 jQuery 插件。不幸的是,它可能不适用于所有浏览器。
还有jQuery Lazy Plugin Loader,它按需加载 jQuery 插件,而不是预先加载。要使用它,您需要为您正在使用的每个 jQuery UI 部分设置延迟加载,如下所示(名称将是您使用的每个部分的函数名称):
您还可以使用 这个关于按需加载 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):
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.
你可以尝试这个新的 jquery 插件。与 yeapnope.js 类似,但更有意义。
http://plugins.jquery.com/plugin-tags/autoloader
You can try this new jquery plugin. Works like yeapnope.js but more make sense.
http://plugins.jquery.com/plugin-tags/autoloader
我不会太担心。文件被缓存。一旦站点中的一个页面加载了 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
听起来你想要一个脚本加载器。
不过,通常无法跨浏览器同步加载脚本,因此脚本加载器必然是异步的。您所要求的并不完全可能,因为脚本需要加载、调用回调,然后继续。您不想调用某个插件而不知道它是否同步执行,这会让您陷入问题的世界。
我建议你看看 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
从您的评论来看,您的部分愿望似乎是保持代码井井有条。我会推荐 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.
是的,我也想过实施这样的事情。我不确定它最终是否值得,但有很多库可以为您执行此操作,例如 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
你可以尝试这样的事情,但这会很痛苦。基本上,您正在检查捕获的错误类型,并在对话框(您尝试调用的函数不存在)加载该函数并尝试再次调用该方法时发出消息。就像我说的,除非想到一些优雅的解决方案,否则在任何地方都这样做会很痛苦。
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.
这是上述评论的后续帖子:
This is a follow-up post for a comment above: