包含外部 javascript 的最佳/最快方法是什么? 我使用 jQuery
我的网站即将完成,目前正在对其进行优化; 我想知道包含所有外部 javascript 文件的最佳和最快方法。 我希望网站能够尽快下载,但它有相当多(10 个左右)的外部 javascript 文件需要加载; 有些是来自 Google 的 AJAX API 的 jQuery 库文件,有些是我的。
我确信我不久前读过,我可以使用一点 JavaScript 代码来调用所有外部脚本,实际上,从浏览器的角度来看,只调用一个外部文件。
你明白我的意思吗?
非常感谢
I am nearly done with my site and am optimising it at the moment; I would like to know the best and fastest way to include all my external javascript files. I want the site to download as quick as possible, but it has quite a few (10 or so) external javascript files that need to be loaded; some are jQuery library files from Google's AJAX API and some are mine.
I'm sure I read a while ago that I could call all external scripts using a bit of javascript code, in effect, only calling one external file from the browsers point of view.
Do you see what I mean?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您所指的(使用 Google 的 AJAX 库服务)是另一种处理此问题的方法,属于 CDN(内容交付网络)的范畴。 这个想法是,文件存储在多个位置,客户端将下载最接近的位置(并且结果将被保存)。
与其他技术结合起来很难或很尴尬,而且我发现以这种方式执行多个外部负载完全削弱了任何感知到的好处(除非这是您唯一的外部负载),所以我改用上面列出的方法。
What you're referring to (using Google's AJAX Libraries service) is another way to handle this that falls under the heading of a CDN (Content Delivery Network). The idea being that the file is stored in multiple plallllces and the client will download the closest (and that result will be saved).
That's hard or just awkward to combine with other techniques and I've found that doing multiple external loads this way completely erodes any perceived benefit (unless that's your only external load) so I use the method listed above instead.
我的猜测是将库文件合并为一个文件(Google 托管的文件除外)。 对服务器的每次调用都会占用相当多的资源,因此您最好只进行一次调用。 您甚至可以在线合并文件:
http://yui.2clics.net/
My guess is to combine the library files to just one file (except the ones hosted at Google). Each call to your server takes quite some resources, so you're better off with just one call. You can even combine the files online:
http://yui.2clics.net/
我建议将所有脚本合并到一个 JS 文件中,然后使用 YUI 压缩机将它们打包成一个较小的文件。
I would suggest merging all the scripts together into one JS file, and then using the YUI Compressor to pack them into a smaller file.
要使用脚本,浏览器必须下载它们,所以我认为如何调用它们没有任何区别......除非您在服务器上使用压缩并在客户端上解压缩。
To use scripts browser has to download them anyway so I don't think there is any difference how do you call them... unless you use compressing on server and decompressing on client side.