tags:

views:

1113

answers:

2

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)

+1  A: 

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.

Hyposaurus
Basically it's an ajax component, so it displays some data and then needs to special JS logic to call an action etc.Which is why I would like it to be self contained; so you can just drop one of them in and be done with it.
SCdF
+4  A: 

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.

Tony BenBrahim