3

I"m wondering if there's a way to load a JSON file without having to use AJAX with jQuery. I already have the json file in the folder, so it would seem a bit wasteful to load the file after everything else is loaded with AJAX. My folders look like this:

css/
font/
html/
img/
config.json

So I would like to have the data available as soon as the js is available instead of having to use jQuery or another method to fetch it later. Thanks in advance. (without declaring it a variable so no var foo = {...})

Philll_t
  • 4,267
  • 5
  • 45
  • 59
  • 2
    just don't wait for document ready to load it? Or include it in the html page source – AD7six Aug 23 '13 at 19:04
  • yeah, that would be ideal, including it in the html src since I have the json file here and not in a remote server. – Philll_t Aug 23 '13 at 19:04
  • 1
    You could read the contents of the file using your server-side language and inject it into the page as the page loads. – Jason P Aug 23 '13 at 19:10
  • It isn't any more or less wasteful to request javascript with a script tag vs an ajax request. the same applies to json(though it isn't exactly possible to request json with a script tag unless it's valid javascript) – Kevin B Aug 23 '13 at 19:34

2 Answers2

1

Add the variable assignment to the JSON file and just use a standard JS SCRIPT tag.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
1

Unfortunately, you can't do what you're talking about. This is a good post that explains your options. https://stackoverflow.com/a/13576588/1397590

Community
  • 1
  • 1
JimTheDev
  • 3,469
  • 1
  • 23
  • 26