Why code and Javascript in the middle of the HTML are if bad thing?
This item is a “” (1) of the excellent item JavaScript Codes has in which Robert explains why it is good to separate structure HTML, presentation and the Javascript interactivity in the process of design of a website. Each party should be the subject of a distinct file where the intrusion of both should be limited to the strict minimum. , ahead young people!
When I review websites, including those to which I take part in collaboration with other people, I often fall on things which the web developers should eliminate from their practice: to put code and Javascript at the right in the middle of the HTML code.
What a style or a Javascript on line?
It is when the HTML code is truffle of and Javascript, thus mixing structure (HTML), the presentation and the interactivity (Javascript). Like this:
" : 800px; : 1em car; make: 1em/1.2 , Arial, , without-serif " >
" : left; : 400px; : 1em 2em; make-size: 0.9em " >
" " " “
Why is this so bad?
Put aside the concepts of cleanliness of the code and facility of reading in general, there exist other less subjective reasons:
Weight of the HTML page - By you of the superfluous elements on line, you optimize your files.
Impossible - the HTML code is not put cached contrary to external files or Javascript. The elements on line are thus systematically charged: the presentation and the interactions present in your HTML code will not be charged as quickly as if they were it starting from the browser cache.
Weak accessibility - the interactions Javascript on line like the event considering higher, are often implemented to elements which does not have a behavior associated by defect (a link has an attribute giving access the resources wished, etc): your link will not carry out nowhere if Javascript is not activated.
Difficulty of maintaining the code - All the web developers will be of agreement to recognize that it is easier to be found there when the code is centralized rather than scattered with the four corners of the website.
Doesn't everyone have Javascript nowadays?
Firstly, not, everyone does not have Javascript. Secondly, certain people voluntarily decontaminate the on their navigator (e.g. the extension Firefox was downloaded illion times to date). Thirdly, in certain cases, the user does not choose his environment and Javascript can not be available. These circumstances are:
The antivirus programs or the fire walls which can be sometimes too severe in their judgment with respect to the Javascript code.
Certain corporate proxy which filters the code (to read important An butt on this subject).
Other tunings of access to Internet specific to certain firms preventing from carrying out the code Javascript under good conditions.
Here how you should develop
A developer of interface normally made up knows that it or it should make the effort produce a structure completely separating the contents (the HTML page itself) from the presentation and the interactions (Javascript). In other words, there should be neither styles on line nor Javascript in the middle of your HTML code.
Only one possible channel: to depend only on the attributes and to start your shares.
Let us rewrite the example properly not to be followed considering higher:
" " " / " " " " "/>
" /Javascript " " "
" container " >
" navigation " >
" " " news-proper-URL "
/* Code in a separate file *
{
: 800px;
: 1em car:
make: 1em/1.2 , Arial, , without-serif;
}
{
: left;
: 400px;
: 1em 2em;
make-size: 0.9em;
}
/* Code Javascript in a separate file *
. {
(“”) = {
//
};
}
The class, not?
To use the attributes and well
At the basis, it is very simple. A appears only once by page, while the attribute can be repeated once as much than it is wished. Roughly speaking, I use the for the large structural blocks, like container, navigation, hand-content, etc For the remainder, I use classes.
To connect code to an element present in the HTML document having an attribute is very simple. However, when it is of Javascript and the access to the DOM in particular, the functions are not always entirely implemented in the navigators. To cure it, I recommend the use of the function which contains some new functionalities in addition.
Handling of the events in Javascript
The code of the last example takes as a starting point the good old method provided for by good old man DOM level 1 to implement shares to an element. That functions very well when it is a question of implementing a share by element. It is less obvious if several events are associated with an element. It there capable high probabilities so that the code of someone else crushes your event, unless it is the opposite!
Not to be caught the head with the differences in implementation of event management between the navigators (and believe me there is some-a), I recommend the use of a Javascript library like or .
And the large ones, like Google, they make how?
Good, now, I succeeded in convincing you of the accuracy of my arguments and you are ready to dive you in the marvellous world of the new methods of development, but just by curiosity, you throw an eye on the most popular website in the world, Google, and you think:
But, one minute, Robert waits, are not to you swindling us in the flour!
The home page of Google and more especially the page displaying the search results, are garnished with truffles styles on line and Javascript events you in here want some. If Google does it, that must be the signal, not? Ben not. Perhaps Google has talented Javascript developers, and even of the geniuses in other fields, but as regards integration HTML and , they are a little with “collects”.
Approximately two years ago, my friend Roger properly rewrote the code of Google by explaining its step in detail in its bank note.
It is necessary to consider the extremely high rate visitors that Google accommodates each day to understand that their watchword is performance, performance and… performance (those which want to look further into the question more deeply can read web site performance - & have rating). But although for the team of Google the elimination of each superfluous request HTTP is crucial, it is not an excuse not to have a clean and valid code.
Of final, I think that the benefit brought by the use of code or Javascript (in the HTML code) is negligible, and in parallel, as soon as we speak attracted and kept visitors (for example, I visit Google the every day), the use of external files would be more beneficial and considerably better for the performances just like for the charge of band-width. Also, in comparison, the page of Google Mobile is in a much better state, therefore the fact that the conventional page either so poor has a valid reason well - - I think that it is due to something which was neglected and not re-examined yet (and they are probably terrified with the idea to re-examine all their page).
Conclusion
Thus, as this text invites you there, ensure you to include all your code and Javascript starting from external files. Start as of today! Move all the elements in line which you will find and you will see that you will feel better, and your team too;)
(1) A “” is a fast and approximate translation which has as a main aim to transmit the message. You can take part in the measurement of your means in the comments or by contacting me directly.
PS: Thank you in Jean for his aid in the last straight line;)
11 participations in the item
Amen! (nothing to tell moreover: p)
In a the use of allows varied presentations bank note by bank note without intervening on the file style, of the presentations which can be single and never not being re-used. It is also a solution the lazy one.
Excel item like always!! And me which have tendency to often adding Ci, from there, Javascript in my pages: S I knew that it was not a good practice but there I am completely convinced, thank you very much!!
(, small error of manipulation' in the comments: -/)
Excel item!
“Weak accessibility - the interactions Javascript on line like the event considering higher, are often implemented to elements which does not have a behavior associated by defect (a link has an attribute giving access the resources wished, etc): your link will not carry out nowhere if Javascript is not activated.”
They is false, Lien If the person does not have Javascript, the , will make so that the page “” will be called, if not (if the user with Javascript) the function is called and the ignored link .
Then one cannot generalized like that, there are cases of figure or the code Javascript is generated he is much easier to put it . is necessary to see the value added to put it in an external file, but it is not always the best method.
Cordially.
@Fryck: Indeed, on a link, that makes it, considering this beacon has - as specifies it the item - a default behavior which will be activated in the place of the .
What the item refers to, is the events on beacons or , etc and there, if is not activated, there is nothing to take the changing, from where lack of accessibility noted by Robert .
http://www.thinkxtreme.com/ and then it is all. : p
You speak about maintenance of code etc, indeed it are well, but would as be necessary to speak about the optimization of the page such as for example, the navigator locks with each thus is better as there is that only one.
And then think of too. Ca helps much. ;)
@Fryck: Thank you for information
Not badly for a “”; a clean and light code should indeed be more effective also for referencing and… Google, this being known as one finds sites coded with old or writings with the feet positioned very well on the ! :)
Good item. I would in the future avoid placing in full medium. I understand better why the display of my site rows much now.
4
It is not well to mix HTML code, and Javascript! …
Bruno on the previous day published on his a bank note explaining why it is not a good practice only to mix the HTML code, the (with blow of attributes style) and the Javascript (with blows of attributes of events of the type of the …
[...] and improve the performances of your site your incomes up to 10 code and Javascript in the middle of the HTML is if bad thing? The signal of the useless but essential softwares! Photographs: when urban furniture [...]
[...] Pourquoi code and Javascript in the middle of the HTML is if bad thing? [...]
[...] WordPress.org JavaScript codes has 23rd, 9000 6:40 amndt has translation in off item. [...]