Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing JavaScript in JSP tags

I have a .tag file that requires a JavaScript library (as in a .js file).

Currently I am just remembering to import the .js file in every JSP that uses the tag but this is a bit cumbersome and prone to error.

Is there a way to do the importing of the .js inside the JSP tag?

(for caching reasons I would want the .js to be a script import)

like image 815
SCdF Avatar asked Sep 08 '08 04:09

SCdF


People also ask

Can JavaScript file contain script tag?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Where do I put script tag in JSP?

As per the answers given, many agreed to place the <script> tags just before the closing body tag.

Can we add JavaScript in JSP?

yes, you can use JavaScript with your JSP pages. use <script> tag.


2 Answers

There is no reason you cannot have a script tag in the body, even though it is preferable for it to be in the head. Just emit the script tag before you emit your tag's markup. The only thing to consider is that you do not want to include the script more than once if you use the jsp tag on the page more than once. The way to solve that is to remember that you have already included the script, by addng an attribute to the request object.

like image 103
Tony BenBrahim Avatar answered Oct 13 '22 15:10

Tony BenBrahim


Short of just including the js in every page automatically, I do not think so. It really would not be something that tags are designed to to.

Without knowing what your tag is actually doing (presumably its its outputting something in the body section) then there is no way that it will be able to get at the head to put the declaration there.

One solution that might (in my head) work would be to have an include that copies verbatim what you have in the head after the place in the head to import tags right up to where you want to use the tag. This is really not something that you would want to do. You would have to have multiple 'header' files to import depending on the content and where you want to use the tag. Maintenance nightmare. Just a bad idea all round. Any solution I can think of would require more work than manually just adding in the declaration.

I think you are out of luck and stuck with manually putting it in.

edit: Just import it in every page. It will be cached and then this problem goes away.

like image 27
Doug Miller Avatar answered Oct 13 '22 16:10

Doug Miller