trixrabbit
trixrabbit

Reputation: 279

Good practice of javascript in jsp pages

From what I read here and there I found mixed opinions on the subject (it may be because of the different uses) and I'm a bit confused.

I recently started doing web programming and I'm using java EE. I want to integrate javascript functions in my code. From what I read the best practice is to put the .js files in a folder and use the <script> tag to refer to them.

From there I have 3 questions :

1) Some people say that I need to put the <script src=...> in the <head>tag. Some says it's best practice to put <script> at the end of the jsp file for performances, which is the best practice?

2) Should I put all my functions in a single .js file or create a .js for each jsp page I have?

3) Do I include jquery the same way as my .js files ?

Upvotes: 1

Views: 1235

Answers (2)

DA.
DA.

Reputation: 40673

  1. Always put your JS file links at the bottom to prevent blocking of parallel downloads.

  2. Depends on how you're using JS. Ideally you'd have a lot of shared JS that you'd want to put into one file so it caches. But if all your JS is unique to every page, there's no benefit to caching.

  3. jQuery is JavaScript, so, yes, you load it just like the rest of your JavaScript.

Upvotes: 0

itsthejash
itsthejash

Reputation: 138

  1. Its up to you, either will work. From my reading/understanding the purpose of adding the script to the end of the body is that you do not block the parsing of HTML by fetching the script(you can also use the async & defer attributes). Here is another topic on the issue.

  2. This is usually dependent on what the JS is doing. You should reuse common functionality and group common things together.

  3. Yes, just make sure you get the order correct. If you are using jQuery in other files or somewhere else on the page make sure you include it first.

Upvotes: 2

Related Questions