Nikhil
Nikhil

Reputation: 785

Loading json file before js file

My application needs static translation. I've entered the translations in a json file. I want to load this json file before the js files are loaded. How can I do it? Any help would be appreciated.

Upvotes: 0

Views: 305

Answers (3)

nitinsridar
nitinsridar

Reputation: 696

$(function(){

  $.ajax({

      dataType: "json",

      url: 'path/to/json/file.json',

      success: function(result){

         // you can process the JSON result first
         // and call other functions after this point

      }

      });

  });

Upvotes: 0

behzad besharati
behzad besharati

Reputation: 6920

you can do something like this:

$(function(){
  $.ajax({
    dataType: "json",
    url: 'path/to/json/file.json',
    success: function(result){
        start(result)
    }
  });
});
function start(data /*converted to javascript object*/ ){
  // you can process the JSON result first
  // and call other functions after this point
}

Upvotes: 0

Stefan Jelner
Stefan Jelner

Reputation: 111

Personally i would not rely on the order, the scripts are loaded in the document, but use a mechanism like the domready-event or even better something like require.js. With require.js you can load your JSON and after that do your JS-stuff.

domready-event: https://learn.jquery.com/using-jquery-core/document-ready/

require.js: requirejs load static JSON file

Upvotes: 1

Related Questions