在js文件之前加载json文件

问题描述 投票:0回答:3

我的应用需要静态翻译。我已经在json文件中输入了翻译。我想在加载js文件之前加载这个json文件。我该怎么做?任何帮助,将不胜感激。

javascript json html5
3个回答
1
投票

我个人不依赖于订单,脚本被加载到文档中,但是使用像domready-event这样的机制,甚至更像require.js。使用require.js,你可以加载你的JSON,之后做你的JS东西。

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

require.js:requirejs load static JSON file


0
投票

你可以这样做:

$(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
}

0
投票
$(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

      }

      });

  });
© www.soinside.com 2019 - 2024. All rights reserved.