简体   繁体   English

加快网站加载

[英]Speed up web site loading

I am looking for the best way to speed up the load time of my js. 我正在寻找加快js加载时间的最佳方法。

The problem is that I am working with a very large site that uses the jquery framework, and what's happening is because the site is also loading, facebook connect, addthis sharing, google analytics and another tracking code, the jquery is delayed a few seconds, and certain elements like the calendar just appear, and my users are complaining that things take to long. 问题是我正在使用使用jquery框架的大型网站进行工作,而发生的原因是该网站也正在加载,facebook连接,addthis共享,google Analytics和其他跟踪代码,因此jquery延迟了几秒钟,日历等某些元素刚刚出现,而我的用户则抱怨事情花了很长时间。

I did a test in google chrome and the avg load time is 4s. 我在谷歌浏览器中进行了测试,平均加载时间为4s。 Which is too much. 太多了

I am already doing minification, and the jquery UI/ Jquery is being loaded from google. 我已经在做小事了,并且从Google加载了jQuery UI / Jquery。 What's the best way to approach this? 解决此问题的最佳方法是什么?

通过结合使用图像,脚本和CSS,减少HTTP调用,还可以使用Content Delivery Network为您提供静态图像,而CSS可能会有帮助!

You are not likely to be able to do much more about the load time of the external scripts, so what you can do is to change the order that things happen in the page so that the external scripts are loaded after you have initialised the page. 您可能无法对外部脚本的加载时间做更多的事情,因此您可以做的是更改页面中发生的事情的顺序,以便在初始化页面后加载外部脚本。

Scripts are loaded and executed in a serial manner, so if you change their order in the source code, you also change the order they are loaded. 脚本是以串行方式加载和执行的,因此,如果您在源代码中更改脚本的顺序,则也将更改其加载顺序。

Instead of using the ready event in jQuery, you can put your initialising code inline in the page, after all the content but before the body closing tag. 除了在jQuery中使用ready事件之外,您还可以将初始化代码内联在页面中,所有内容之后,正文关闭标签之前。 That way the elements that you want to access are loaded when the script runs, and you can put external scripts below the initialising code to make them load after. 这样,您可以在脚本运行时加载要访问的元素,并且可以将外部脚本放在初始化代码下方,以使它们在之后加载。

Small technical changes (such as serving the JSs from Google, minifying, etc..) will only get you so far. 细微的技术更改(例如从Google提供JS,缩小等)都将使您步入正轨。

Seems you simply have lots of dynamic stuff going on in your page. 似乎您的页面中只是发生了许多动态的事情。 Have you though of an asynchronous way of building your content? 您是否有异步的方式来构建内容? One option might be to place placeholders instead of the external content, and asynchronously load it, so when all the scripts are loaded and ready, all you need to do is throw the markup into the placeholder. 一种选择是放置占位符而不是外部内容,并异步加载它,因此,当所有脚本都加载并准备就绪时,您要做的就是将标记放入占位符中。

This will create a better user experience, instead of the user waiting 10 seconds for the entire page, it will start loading incrementally after 2 seconds (and still fully load after 10). 这将创建更好的用户体验,而不是用户等待整个页面10秒钟,而是2秒钟后开始逐渐加载(并且10秒钟后仍完全加载)。

In addition to Yuval's answer some options that might or might not bring you a speed gain: 除了Yuval的答案以外,某些选项可能会或可能不会为您带来速度上的提升:

  • the load time of external libraries is something beyond your control. 外部库的加载时间是您无法控制的。 Try to include them as late as possible, and better still, dynamically after the page has loaded. 尝试在页面加载后尽早包括它们,最好还是动态添加它们。 This way your page won't stall, if Google analytics or Facebook have another hickup. 这样,如果Google Analytics(分析)或Facebook还有其他问题,您的页面就不会停滞。

  • It is not necessarily faster to load jQuery from Google. 从Google加载jQuery不一定更快。 Consider putting jQuery, jQuery UI and as many of your own JS as reasonable in a single file, minify and gzip it and let the server serve the gzipped version where possible. 考虑将jQuery,jQuery UI和尽可能多的自己的JS放在一个文件中,并对其进行最小化和gzip压缩,并在可能的情况下让服务器提供压缩版本。 Note here , that the gain in speed depends largely on what your users cache and where they cache it. 请注意 ,速度的提高很大程度上取决于用户缓存的内容以及缓存的位置。 If they already have jQuery from Google in their cache, this technique might make page load slower. 如果他们的缓存中已经装有来自Google的jQuery,则此技术可能会使页面加载速度变慢。

The bottomline is, that after some optimization you're out for experimenting. 最重要的是,经过一些优化后,您就可以进行实验了。 You must find out, what your average user has in her cache, if the page is accessed directly via deep links or if you can smuggle some JS or CSS (or even images) into her cache via a previous "landing page". 您必须找出普通用户在缓存中拥有的内容,或者是否可以通过深层链接直接访问该页面,或者是否可以通过先前的“登录页面”将一些JS或CSS(甚至图像)走私到其缓存中。

Make sure you deliver your content in gzip/deflate encrypted format. 确保以gzip / deflate加密格式交付内容。 Combine multiple javascript files into 1 file, which helps to reduce the number of http requests. 将多个javascript文件合并为1个文件,这有助于减少http请求的数量。

PS Here's a test tool to check if compression is configured: http://www.gidnetwork.com/tools/gzip-test.php PS这是一个用于检查是否配置了压缩的测试工具: http : //www.gidnetwork.com/tools/gzip-test.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM