Should JavaScript only load when needed?

I am using jQuery for a project and probably have a dozen additional jQuery plugins / add-ons (tipsy, unified, jeditable, fancybox, etc. etc.).

As a best practice, should you only load pages when they are needed?

Or is it better for me to just download them all from the very beginning, so that they are in the browser’s cache, and you just need to do it instead of programmatically including the code when necessary?

+3
source share
5 answers

It depends. You have to make a decision depending on the size and time of downloading files and without them. If all this is a reasonable size with files, you can just include them all. Otherwise, turn them on if necessary. Or you can even turn them on at different times on the user's path through the site, so when they reach a page that requires a lot of them, they don’t have to download everything right away. Unless you're talking about large file sizes (which I doubt), this probably won't make much difference anyway.

+1
source

In general, you should only load scripts that are required on the page.

Each added file is an additional call to your server (provided that it does not extinguish), adding load and traffic.

, .

, , ?

, , , , .

0

, , , - 5 , minify request . .

, :

<script type="text/javascript" src="http://www.tylerbel.com/workspace/min/f=workspace/js/jquery-1.4.2.min.js,workspace/js/jquery-ui-1.8.5.custom.min.js,workspace/js/main.js">
0

, PHP, , . , , , js , php. php js script , if. , , , . , $_SESSION ['chatjs'] = '1';... php js if (isset ($ _ SESSION ['chatjs'] )) {include ('whatever.js); ($ _ SESSION [ 'chatjs']); }, 0,035 , , .

0

Source: https://habr.com/ru/post/1783878/


All Articles