This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes read a property file to set variables using javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply locked New topic
Author

read a property file to set variables using javascript

Anne Forumer
Ranch Hand

Joined: Apr 19, 2004
Posts: 72
I'd like to set some variables in a javascript by reading a properties file. Specifically, I need to determine the environment, like what server etc, various javascripts are operating in. This will be done on the server side.

How can I do this?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

What part are you having problems with? Reading a properties files? Generating Javascript using read-in values? What code have you already written?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anne Forumer
Ranch Hand

Joined: Apr 19, 2004
Posts: 72
I just don't know how to read a property file using javascript!

Ultimately, I'd like to have a site-specific file which javascript scripts could use to set variables specific to that site. It may be done by reading or, somehow, "including" that file. I don't know much javascript.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
use an external javascript file
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

You said:

This will be done on the server side.


then you said

I just don't know how to read a property file using javascript!


These are mutally exclusive.

You can read the server-side properties file in your servlet or JSP to produce the appropriate Javascript variable initializers with the values read from the properties file, or you can just include different JS files (not properties files) as Eric suggested.

You can't mix the two. Javascript has no access to the file system (either on the server or client side) and cannot read files of any type.
Anne Forumer
Ranch Hand

Joined: Apr 19, 2004
Posts: 72
I am using a third-party application which allows me to place my scripts only in a single file, common.js. I want to place the same file on each of my installations. However, I'd like scripts in common.js to refer to variables which will have installation-specific values.

What is the best way to accomplish this, given my constraints.

Thanks
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi Ranchers,

I am facing a similar problem.

In my web application, I have a properties file that has many name-value pairs.

I want to use the names in properties file in a javascript function.

Eg:
1)
system.properties file:
jdbcurl = jdbcracle:thin:@myhost.com:1520:TID007z

2) javascript function:
function testfunction(jdbcurl){
var connectionurl=jdbcurl;
// some processing with connectionurl
}


So, how can i load jdbcurl from the system.properties file?

Any suggestions are welcome.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Same answer as given previously: you'll have to read the properties on the server side and create the appropriate JavaScript markup to send to the browser.
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi Bear,

Thanks for your prompt reply.

Can you give some example?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Actually, since this is a sever-side issue, and because this original is over 4 years old, I'm going to ask you to re-post your request for examples in the JSP or servlets forum. It's rather off-topic for this one, and I can't move this older post.
 
Consider Paul's rocket mass heater.
 
subject: read a property file to set variables using javascript
 
Similar Threads
Storing field value in java properties file.
Javacodes in JavaScript
jsp and javascript
setting hidden fields
Need Some Help Here