开发者

Is it possible to read values from a JSON object stored in another javascript file?

开发者 https://www.devze.com 2023-01-07 02:39 出处:网络
I am working on a custom piece where I am dyna开发者_如何学Gomically building very specific tables and writing them out using javascript after an AJAX call.I am trying to avoid writing custom code for

I am working on a custom piece where I am dyna开发者_如何学Gomically 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?


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.


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


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
});
0

精彩评论

暂无评论...
验证码 换一张
取 消