0

So my problem is the next one:

I'm trying to get text from a JSON file, on my html on the header I linked a tag that has:

var language = "";
var langDoc = null;

//Function to change the value
function setLang() {
    var myCookie = getCookie("language");

    if (myCookie == null) {
        document.cookie = "language=español";
        language = "español";
        $.getJSON("/lenguajes/" + language + ".json", function(data) {
            langDoc = data;
        });
    }
    else {
        language = myCookie;
        $.getJSON("/lenguajes/" + language + ".json", function(data) {         
            langDoc = data;
        });
    }
}

//Final function to get text from file
function getTextFromJson(textToGet){
    return langDoc[textToGet];
}

And I'm trying to call the setLang(); function before calling my getTextFromJson() one, to get the text from the modified variable, but when I do it, the functions says that can't get the property "IDIOMA" from null, so the variable is not changing, the HTML looks like this:

<script type="text/javascript"> setLang(); </script>
<script type="text/javascript"> alert(getTextFromJson("IDIOMA")); </script>
<!-- other HTML  -->

And the idea is to be able to use that method on other parts of the HTML to make a multilanguage page

Thank you!

3 Answers3

0

$.getJSON is an async method so the cause can be about the JSON is still not loaded when you are calling your getTextFromJson function so call this function from the success callback example is as follows:

var language = "";
var langDoc = null;

//Function to change the value
function setLang() {
    var myCookie = getCookie("language");

    if (myCookie == null) {
        document.cookie = "language=español";
        language = "español";
        $.getJSON("/lenguajes/" + language + ".json", function(data) {
            langDoc = data;
            getTextFromJson("");
        });
    }
    else {
        language = myCookie;
        $.getJSON("/lenguajes/" + language + ".json", function(data) {         
            langDoc = data;
             getTextFromJson("");
        });
    }
}

//Final function to get text from file
function getTextFromJson(textToGet){
    return langDoc[textToGet];
}

On the other case, you can also change your $.getJSON method to synchronous but this going to be deprecated soon. So try not using that. More details can be found here.

Ullas Hunka
  • 2,119
  • 1
  • 15
  • 28
0

can't get the property "IDIOMA" from null

Looking at you code, looking at your code, we can tell rightly that langDoc has the initial value null

language = ""
langDoc = null

The Point

When setLang() is called from the Chrome browser console, this throws an error like this:

Uncaught ReferenceError: getCookie is not defined
    at setLang (<anonymous>:6:20)
    at <anonymous>:1:1
setLang @ VM2043:6
(anonymous) @ VM2045:1

an error because getCookie is not defined, so langDoc remains null, and getTextFromJson returns error like this:

Uncaught TypeError: Cannot read property 'IDIOMA' of null
    at getTextFromJson (<anonymous>:25:19)
    at <anonymous>:1:7

SOLUTION The getCookie function to fetch the cookie from the appropriate destination, so that this won't throw an error.

EXAMPLE

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Erisan Olasheni
  • 2,395
  • 17
  • 20
0

$.getJSON is asynchronous so it does not hold the execution and your next script is called before the data is returned from JSON call

Better way is to Handle with the promises.You can easily handle error, success cases.

Also there are some code optimization in your if and else condition

var language = "";
var langDoc = null;

//Function to change the value
function setLang() {
    var myCookie = getCookie("language");
    if (myCookie == null) {
        document.cookie = "language=español";
        language = "español";      
    }
    else {
        language = myCookie;      
    }
   var url= "/lenguajes/" + language + ".json";
   var myPromise = $.getJSON("url")
   myPromise.done(function(data) {
     //Handle the success case here
     langDoc=data;
   });

  myPromise.fail(function(reason) {
     //Handle the fail case here
   });
   //After success you can call your function 
   myPromise.then(function(data) {
     langDoc=data;
     getTextFromJson(data);
   });
}

//Final function to get text from file
function getTextFromJson(textToGet){
    return langDoc[textToGet];
}
NullPointer
  • 7,094
  • 5
  • 27
  • 41