Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

read a property file to set variables using javascript

 
Anne Forumer
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
What part are you having problems with? Reading a properties files? Generating Javascript using read-in values? What code have you already written?
 
Anne Forumer
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Report post to moderator
use an external javascript file
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Bear,

Thanks for your prompt reply.

Can you give some example?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
    Bookmark Topic Watch Topic
  • New Topic