You really don't need your .js files to be stored as .js if their content type is text/javascript . But having dynamic information in your .js files is wrong:
- you cannot cache them correctly
- you may be tempted to add jsp logic to your .js file, which will be difficult to maintain
- you cannot use competing networks (if necessary)
- (and maybe there are other flaws that I can't think of right now)
Instead, you should initialize some settings object from the jsp page that uses the .js file. See this answer for more details.
Here is a specific (simplified) example from my code. This snippet is in .jsp :
<script type="text/javascript"> var config = { root : "${root}", language: "${user.language.code}", currentUsername: "${user.username}", messages : { reply : "${msg.reply}", delete : "${msg.delete}", loading : "${msg.loading}", } }; init(config); </script>
init(config) is in the .js file and simply sets the configuration object as a global variable. (Actually, I have default values, but that doesn't matter)
Bozho source share