@johnnygerard
Posted
Hello Nrosta,
I opened a pull request to improve your JavaScript code.
Inline scripts should be favored over external scripts when the amount of code is small. This saves an HTTP request.
I also changed a little bit your HTML and renamed rate
to rating
.
Tell me if you find my PR useful.
Good day!
Marked as helpful
@Nrotsa
Posted
@johnnygerard Thank you very much. Your PR is very useful and it is good to know that it is ok to use an inline script when code is small.
@johnnygerard
Posted
@Astornek Thanks for your reply. I'm glad that you found it useful.
I'd like to clarify that the renaming from rate
to rating
is somewhat subjective. In general, verbs like rate
are best for functions or methods. rateButton
is a good name because it refers to an action like sortButton
. Class names are selectors and do not refer to an action, which is why I prefer rating
.
The choice between inline vs external script is also not clear-cut.
In your case, you put your script at the end of the body. The HTML parser downloads it only at the end. An alternative is to put the external script in the head
with a defer
attribute so that it is downloaded as soon as possible but executed after HTML parsing. You would not have to use DOMContentLoaded
event.
You can learn more about this in the HTML standard.