I am new to web development. While studying the Play framework and JSP, I notice
ID: 650771 • Letter: I
Question
I am new to web development. While studying the Play framework and JSP, I noticed that they both give an option to place script (in jsp scriptlet) inside HTML, but in both books I am reading they both say it is bad practice and don't give an explanation as to when should I use scripts, only writing not to.
My question is, if both frameworks supply such an option, when would it be ok to use scripts inside HTML?
Please note, I'm talking about server side scripts for creating dynamic HTML (i.e. JSP scriptlet).
Explanation / Answer
There are mostly two reasons to put scripts outside HTML code:
Server-side, separation of concerns and clean code. You quickly understand why you must be very careful with that when you see some PHP code which, in the same piece of code, mixes PHP, SQL queries, JavaScript, HTML, CSS and Base64-encoded images embedded in CSS.
Client-side, external scripts are easily cached. If your HTML is a dynamic page, it may change often. By putting scripts outside HTML, you allow your visitors to download this content once, and never refresh it again (well, not never, but rarely). In the same way, if two static or dynamic HTML pages share the same script, putting it in an external file will allow to download it once.
? The first point is valid in all cases. You'll never be able to justify that it's cleaner to mix everything in one file. Why frameworks allow this? Because there are situations where you don't care about clean code:
If I write a small page I'll throw away for sure, and I must do it quickly, it's not so bad to put everything in one file.
If I want to show a short example to my colleague (by putting a single file to a document management system platform) or publish it to a blog
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.