views:

50

answers:

1

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.

+2  A: 

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.

Teja Kantamneni
Re `contentType`: can and *really should*.
T.J. Crowder
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. :-)
msharma