-1
Thinking about the concept of HTML 5 and modern web standards we have that in a web page, the HTML file should provide the structure, the CSS style file should provide the formatting and the Javascript should provide the behavior of the page.
Looking at it this way, how should we split our javascript code? A file for each page, an "archive" with all events and methods of all pages?
We should have a file for every web page .html
and a .js
separated?
What is your recommendation?
[UPDATING QUESTION 05/03/2014]
I understand that it is not a question that will have a right or wrong but my goal is to collect the largest number of different opinions and take my conclusions based on the comments and answers of colleagues.
To answer some will consider reusability of code, others will consider performance, others maintenance, etc.
My question is not about using existing JS frameworks but my own routines.
For example, let’s say I have a page that has a combobox and I want that when selecting a value something happens on the page, either manipulate other objects via DOM or perform some remote operation with Ajax. Somewhere on my page I will have a javascript to run this. My question then applies: for this type of javascript code, I create a file per page, leave the JS in the html file (cshtml, php, whatever) or put all these little routines in a single js and Linko to all pages, or any other variation of it.
I find a valid discussion that can add value to many.
What do you recommend?
This depends a lot. Among other things, on the structure of the application. You can split the code into dozens of js files, and compile a single, minified file to be delivered to the browser. In general, the fewer files you deliver to the browser, the faster the app.
– bfavaretto
It became vague, the answer will depend on individual opinion.
– Gabriel Gartz
As @bfavaretto comments the "best practice" can range from a large minified file to a complex modular structure with multiple files (a la AMD). You will find people advocating one, the other or neither according to the characteristics of the problem, background who is responding and personal opinions.
– Anthony Accioly
In my opinion, what is explicit in the question is quite objective ("how to divide the code"). Only what is implied is that it is subjective ("how to divide from the point of view of performance" vs "how to divide from the point of view of logical organization" vs other points of view). That is, it is not the case to close, just edit it by putting a little more context (i.e. focusing on what can be answered objectively). P.S. will remove the "best-practice" metatag, other relevant tags can be added later depending on the context.
– mgibsonbr
Each framework you use will have its best practices, being that Angularjs, Polymer-project, Backbone, Extjs, Meteorjs, and several others, each will be different. So it’s at least vague and based on opinion. The answer will be vague tb, something like MVC, HMVC or Web Components style, all talk to join files and minify them. But they do not fully answer the boy’s question. If he specifies the environment he wants to organize it may be possible to help him.
– Gabriel Gartz