变量无法按预期运行JavaScript-HTML

时间:2018-07-21 06:03:22

标签: javascript html json variables null

所以我的问题是下一个:

我正在尝试从JSON文件中获取文本,在我链接的标头的html上的html上,该标签具有:

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];
}

我正在尝试调用setLang();函数在调用我的getTextFromJson()一个函数之前,先从修改后的变量获取文本,但是当我这样做时,函数说无法从null获取属性“ IDIOMA”,因此变量没有改变,HTML看起来像这样:

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

想法是能够在HTML的其他部分上使用该方法来制作多语言页面

谢谢!

3 个答案:

答案 0 :(得分:0)

$.getJSON是一个异步方法,因此原因可能是在调用JSON函数时getTextFromJson仍未加载,因此从成功回调示例中调用此函数如下:如下:

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];
}

在另一种情况下,您也可以将$.getJSON方法更改为同步方法,但是此方法将很快被弃用。因此,请尝试不使用它。更多详细信息,请参见here

答案 1 :(得分:0)

  

无法从空获取属性“ IDIOMA”

查看您的代码,查看您的代码,我们可以正确地判断langDoc的初始值为null

language = ""
langDoc = null

要点

从Chrome浏览器控制台调用setLang()时,会引发如下错误:

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

由于未定义getCookie而导致错误,因此langDoc仍为null,并且getTextFromJson返回如下错误:

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

解决方案 getCookie函数可从适当的目标位置获取Cookie,这样就不会引发错误。

示例

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 "";
}

答案 2 :(得分:0)

$.getJSON是异步的,因此它不保存execution,并且在next script返回data之前将调用您的JSON call

更好的方法是处理promises。您可以轻松处理errorsuccess情况。

您的code optimizationif条件中也有一些else

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];
}