0

I am wondering how javascripts get included in a jsp - can we put any code which the jsp will recognize and not just javascript code only in the .js file?

I have some common javascript code which needs to get executed on different pages, so I decided to place it in its own separate .js file and include it on all jsps which call that function.

The js function now refers to a key from a properties file and some other non-javascript code:

function openPrivacyStmntWindow(){
var url = <h:outputText escape="false" value="\"#{urls.url_privacyStatement}\";" />
newwindow=window.open(url,'Terms','height=600,width=800,left=300,top=100,scrollbars=1');
newwindow.focus();
return false;

}

This function worked just fine when it was included in the jsp itself. Now that I have separated it into its own file it doesnt, do I need to include the properties bundle in this file.

The value="\"#{urls.url_privacyStatement}\";" is referring to a bundle called "urls" which has a key called "url_privacyStatement"

Also in Line 1 var url = <h:outputText escape="false" value="\"#{urls.url_privacyStatement}\";" /> the <h:outputText escape="false" ... /> will it cause any issues?

Thanks.

asked Mar 19, 2010 at 16:56
1
  • You should escape anything you put in a value attribute. Commented Mar 19, 2010 at 17:43

1 Answer 1

2

You can't use custom tags in your js files. But you can extract this to a separate jsp page and include it in all the pages. You can also set the contentType for that jsp to be text/javascript.

T.J. Crowder
1.1m201 gold badges2k silver badges2k bronze badges
answered Mar 19, 2010 at 17:00
Sign up to request clarification or add additional context in comments.

2 Comments

Re contentType: can and really should.
This works beautifully! I placed my js function in a jsp page with content only javascript. A very smart way of getting around the problem, many thanks. :-)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.