Should i put all my javascript in one file?
For each file you have, there are two steps : Show
If you reduce the number of files by combining them, you will reduce the number of HTTP requests -- which means your page will load a bit faster ;; which is good for your users ; which is why it's recommended.
Of course, the process of combining your files content should not be done manually -- else, you'll have to re-do it each time there's a modification made ; it should be fully automated, and done at the time you are building the archive that is going to be deployed on your production server.
Ideally, you can use all three solutions, btw ;-)
This can help too -- but might be a bit harder to achieve than combinaison+minification+compression. Can I please have this JavaScript file? Cheers. Now can I please have this JavaScript file? Cheers. Now can I please have this JavaScript file? Cheers. Now can I please have this JavaScript file? Cheers. Now can I please have this JavaScript file? Cheers. Now can I please have this JavaScript file? Cheers. Now can I please have this JavaScript file? Cheers. Now can I please have this JavaScript file? Cheers. VS Can I please have this JavaScript file? Cheers. I am trying to add some JS functionality to a second page of my website, but work off the same JS file for both the first and second pages. When I start up the second page on Localhost, however, I get a whole bunch of errors because the JS script is looking for divs, elements on the first page which it can't find (and vice versa). My understanding is that it is better to have ONE JS file and have it accessed by your various HTML files but then I am getting all manner of errors and headaches. So is this correct? Or can I have seperate JS files for each of the HTML files (like tag-teams)? I have read around the internet that for deployment it is better to have one big JS file but then I am stuck back at my original problem... One of the difficulties I'm running into with my current project is that the previous developer spaghetti'd the javascript code in lots of different files. We have modal dialogs that are reused in different places and I find that the same .js file is often loaded twice. My thinking is that I'd like to just load all of the .js files in _Layout.cshtml, and that way I know it's loaded once and only once. Also, the client should only have to download this file once as well. It should be cached and therefore shouldn't really be a performance hit, except for the first page load. I should probably note that I am using ASP.Net bundling as well and loading most of the jQuery/bootstrap/etc from CDN's. Is there anything else that I'm not thinking of that would cause problems here? Should I bundle everything into a single file?
Rocklan 4,2741 gold badge14 silver badges28 bronze badges asked Jul 14, 2014 at 21:51
1 Depending on the size of the application, it might be worthwhile to modify your app to use modules with an AMD loader, such as RequireJS. By moving your code into separate modules and letting an AMD loader manage the dependencies, your code should become more organized and prevent the issue of you having to manually load files. You can use this for files that are hosted on your server, as well as files being served from a CDN. Also, here's an article explaining how to use RequireJS with an ASP.NET app:
answered Jul 21, 2014 at 21:47
AlexanderAlexander 3781 gold badge5 silver badges14 bronze badges The only possible downsides to bundling all of your JS into one file that I can think of are:
Having said all that, I say go for it. I don't think the above are much of a concern. You'll very quickly see what the issues will be :) answered Jul 21, 2014 at 23:47
RocklanRocklan 4,2741 gold badge14 silver badges28 bronze badges Should all my JavaScript be in one file?To avoid multiple server requests, group your JavaScript files into one. Whatever you use for performance, try to minify JavaScript to improve the load time of the web page. If you are using single page application, then group all the scripts in a single file.
Should I combine JavaScript files?Combining files can be better for performance #
For my bigger plugins, I like to work with a handful of smaller, more modular files instead of one massive 3,000 line JavaScript file. Sometimes, doing so also saves me a lot of work and keeps my code more DRY.
Can JavaScript be in a separate file?You can keep the JavaScript code in a separate external file and then point to that file from your HTML document.
Where should I put my JavaScript files?JavaScript in or
You can place any number of scripts in an HTML document. Scripts can be placed in the , or in the section of an HTML page, or in both.
|