/    Sign up×
Community /Pin to ProfileBookmark

Change array contents from external file?

[COLOR=”Red”][SIZE=”5″]This is a “can I” or “is it possible” question:
[/SIZE]
[/COLOR]

If I have one main html file that calls an external JS file that contains only an array (infoArray), know I can do this

Code for “Main.html”

[code]
<html>
<head>
<title>Main File</title>
<script type=”text/javascript” src=”infoArray_file1.js”>,/script>
</head>
<body>
<!– … code to use and display information in infoArray… –>
</body>
</html>
[/code]

Code for “inforArray_file1.js”

[code]
var info = [
[‘Destrict1′,’State1’],
[‘Name1′,’City1′,’Phone1’],
[‘Name2′,’City2′,’Phone2’],
[‘Name3′,’City3′,’Phone3’],
[‘Name4′,’City4′,’Phone4’],
[‘Name5′,’City5′,’Phone5’]
]
[/code]

If later I then create a different external infoArray_file with different information, different length, etc.
Code for “inforArray_file2.js”

[code]
var info = [
[‘Destrict2′,’State2’],
[‘Name10′,’City10′,’Phone10’],
[‘Name20′,’City20′,’Phone20’],
[‘Name30′,’City30′,’Phone30’]
];
[/code]

[COLOR=”Red”][SIZE=”5″]Now the question…[/SIZE][/COLOR]
Instead of 2 different main.html files, to load the different inforArray.js files, can I somehow overwrite the contents of the infoArray
AFTER the information has been loaded from the first external JS file?

I have seen code that adds elements to the <head> section as the page loads,
is there something similar to load/change array information during or after page load?

Or is this something that should be done by having text information in the file and loading with ajax and processing to an array afterward?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinApr 22.2010 — You can load another .js file and run your function again with the newly defined (overwritten) variables?

[code=html]function loadJS(url){

var e = document.createElement("script");
e.src = url;
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);

}
[/code]
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...