Read Property Value in JavaScript File

Posted on 2011-04-25
Last Modified: 2012-05-11

I have a properties file in my Java web application that consists of key/value pairs and gets placed in the classpath so my application can ready configuration values from it.  I can't figure out how my JavaScript files can read the values, given the keys of the properties file.  I would like to have a 100% configurable application and not have to hardcode any string values in my JavaScript files.  I access the values of the properties file like below for xml files....

Question by:cgray1223
    LVL 47

    Expert Comment


    You are writing that you have Java Web application - so it uses servlets or JSP
    pages or somthing of the sort?

    So you mean you JavaScript will be created in your servlet code?

    Could you please, elaborate a little more on your architecture - how
    does your Java Web application relates to the javaScript which you are mentioning?

    Author Comment

    I have a JSF based application that runs on tomcat, so its a Java based application that conforms to the Servlet spec....It has html pages...etc.  Some of my html pages include a javascript function.  In that function it has the need for some default values, like a url or a redirect..etc.  I would like to externalize those values instead of baking them into the javascript.  I have a .properites (ResourceBundle) in the Java classpath that I can call from Java or xml, is there a way to call it from a js function?
    LVL 47

    Expert Comment

    It is probably something rather difficult - muind that your javascript executes on the client,
    and your property file is on the server.
    So inless you want to send the whiole of your property file somehow with the web page to
    the client you'd hardkly be able to read it directly from javascript.

    You can read it before when preparing the page and include the value in the page - that is waht you don't want

    You can set up some ajax like connection back to server from your java script and web service would return to java script
    the value, but that seems to be like an overkill
    LVL 92

    Expert Comment

    you can use jsf/jsp/servlet to generate the javascript the same way you generate html
    just map the js path to the appropriate backend component
    LVL 20

    Accepted Solution

    you can use foramt tag....

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This video teaches viewers about errors in exception handling.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now