Including many javascript files on the same page can lead to low performance. What I want to ask is: Is it best to keep separate files or include all files in one javascript file?
And if it is better to include everything in the same file Javascript, how can I avoid conflicts between different scripts?
It is best to keep separate files or include all files in one file Javascript?
To avoid multiple server requests, it is better to group all your JavaScript files into only one.
If you're using C#.Net + ASP.Net, you can bundle your files — it is a very good way to compress your scripts.
how can I avoid conflicts between different scripts?
You can use RequireJS; different names it is a good idea too; and every time that a plug-in/script ends, use ;
to determine its end.
To maintain the productivity of your scripts, you should minify them.
After you have minified your scripts, then group them all — regardless if they are plugins or your own code — into a single file and call it in your application.
In this case, you should call just the necessary JavaScript for each page. How can you do this?
After you minified all of your scripts, then separate them in categories. Take a look to get the idea:
/assets/js/core.js
— here you put your JavaScript code that is necessary to all of your pages. It is your core; your kernel;
/assets/js/plugins.js
— here you put all the plugins that runs with all of your pages (i.e. jquery, backbone, knockout, and so on.);
/assets/js/home/bootstrap.js
— here is the magic: in this file you have to fill with the code that your home will call;
/assets/js/contact/bootstrap.js
— the same as before: this file you should to place with the code that your contact page will call.
HINT: core.js and plugins.js can occupy the same file when you deploy your application. To better comprehension and developing, it is good to maintain them into separated files.
To give you an idea, the file of your plugins should be like this:
/* PlaceHolder plugin | http://www.placeholderjs.com/ | by @phowner */
; $(function(){ /* the placeholder's plugin goes here. */ });
/* Masked Inputs plugin | http://www.maskedjs.com/ | by @maskedowner */
; $(function(){ /* the masked input's plugin goes here. */ });
/* Mousewheel plugin | http://www.mousewheeljs.com/ | by @mousewheelowner */
; $(function(){ /* the mousewheel's plugin goes here. */ });
Serving one large single vs serving multiple small files depends on quite a few factors:
Conflicts can be reduced/eliminated by introducing your own scope for each script. A common way is to use IIFEs/SIAFs and inject required variables.
A quick and simple example of an IIFE:
(function () { // IIFE
var undefined = true;
})();
if (document.querySelectorAll === undefined) {
// polyfill
}
If the content in the IIFE would execute in global scope, it would probably crash your polyfill. Since it is executed in a local (function) scope, you are pretty much safe from conflicts.
Note: usually, your polyfill should not be implemented in global scope as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With