Using external .js file array in separate html file

Google Maps Multiple Markers

I want to put the array
var locations = [
[‘J1156’, -33.67047492, 151.11834768, 4],
[‘J8877’, -33.67050625, 151.11855934, 5],
[‘J4443’, -33.67045524, 151.11878817, 3],
[‘j5527’, -33.67061401, 151.11899238, 2],
[‘Maroubra Beach’, -33.950198, 151.259302, 1]
];

into a separate .js file but be able to access it from the html file. Can anyone suggest what I should be doing please.

Terry

Hi @tblack, you might expose the array to the window object:

window.locations = [
  // ...
]

… and then access window.locations from anywhere. However that’s not really good practice (also called “global namespace pollution”); for instance, there’s already a window.location property, so you have to be really careful to avoid such collisions.

Why would you want to access that array from the HTML in the first place though? If you have other JS elsewhere in the document, just put it into the separate JS file as well. :-)

1 Like