Ken, you can make a synchronous request with XMLHttpRequest if you need to
(though it's not commonly done):
var request = new XMLHttpRequest();
request.open('GET', 'data.json', false); // false implies synchronous
But do consider the option of making an asynchronous request and putting
the rest of your code in the request callback function so it executes only
when the data has loaded.
On 7 April 2016 at 11:02, Ken Irwin <[log in to unmask]> wrote:
> Hi folks,
> data defined inside the script; I'd like to move the data to a file outside
> the JS file so it can be updated without touching the script, but I'm
> running up against a few challenges externalizing the data.
> The static JSON file lives in the same directory with the script.
> If I had my druthers, I'd to it PHP style, but I don't think JS works this
> External file:
> [all this JSON]
> var data = include ('data.json');
> All the options I find for loading external files are all AJAX-y, whereas
> what I really want is something synchronous - the script doesn't go on
> until the data loads.
> I've also had some lexical scope issues where I can get the data inside
> the getJSON() function, but then have trouble transporting the data out of
> that function into the rest of the script.
> Does anyone know of a good way to accomplish this? I imagine there's some
> incantation that I can perform, but I'm struggling to find it.