views:

33

answers:

3

I am working on a custom piece where I am dynamically building very specific tables and writing them out using javascript after an AJAX call. I am trying to avoid writing custom code for each of these tables by coming up with a standard layout where I customize the layout via values in a JSON object stored in my current javascript file. Is there anyway I can store this object in another file and read it as if it were a properties file to make things neater?

A: 

You need to dynamically load your additional javascript files. Here is a good article that portrays how to do that using static / dhtml methods.

m_oLogin
A: 

Like m_oLogin said, you can dynamically load the additional scripts. Instead of doing as the article suggested, you can also use jquery to load the scripts -

 $.getScript('script/loaded-script.js'), function() {
//action to execute after script is loaded
});
Dan Appleyard
I ended up doing something similar to this except I didn't use JQuery, I wrote my own function to load my JS files that contained my configuration information; this is probably not the best way to load configurable data but it did allow me to define objects dynamically and process them.
amickj
A: 

Javascript stored in separate files can be referenced as long as it is not wrapped somehow in a function, if so then you would need to reference that function. Thus JSON objects stored in separate files can be referenced - they are just Javascript (everything is an object in Javascript).

SO if you have simple object, or complex JSON:

myobject = "fred";
myobject2 = "wilma";

They can still be referenced, if they are in the same file, or not.

alert(myobject + myobject2); 

shows "fredwilma"

Of course JSON objects would be more complicated, but the principle still applies.

Mark Schultheiss
This is probably most closely related to what I was trying to do; I ended up including my secondary JS files on page when needed by creating a load method that wrote out an include on page so I would have access to my predefined JSON objects.
amickj