0

I cannot create a global variable in a function

function getFabrics(){
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "af_general.php?action=fabrics", true);
    xhttp.send();
    xhttp.onreadystatechange = function() {
        if (this.readyState === 4 && this.status === 200) {
            fabrics_json=JSON.parse(this.responseText);
            console.log(fabrics_json);
        }
    };
}

and use it in another:

function addrow(){
    getFabrics();
    var table=document.getElementById('stoixeia');
    var rows = table.getElementsByTagName("tr").length;
    var row = table.insertRow(-1);
    var cell1 = row.insertCell(-1);
    var rowno=rows-1;
    var pcs_ent=document.createElement('input');
    setAttributes(pcs_ent,{'type':'number','id':'pcs.'+rowno,'name':'pcs','style':'width: 4em;text-align:right','min':'0','max':'99','autocomplete':'off'});
    cell1.innerHTML='#'+rowno;
    cell1.appendChild(pcs_ent);
    var cell2 = row.insertCell(-1);
    var fabric_ent=document.createElement('select');
    console.log(fabrics_json);//returns undefined
    for (f in fabrics_json){
        option = document.createElement('option');
        option.value=fabrics_json[f][0];
        option.text=fabrics_json[f][1];
        fabric_ent.add(option);
    }
    //and goes on...
}

I have read all possible questions in stackoverflow but I can't find a way to get the results. fabrics_json remains undefined either if I declare it in the beginning of the script or not.

2

2 Answers 2

1

Global variables in JavaScript are members of window object. Refer to your global variable as window.fabrics_json.

If both functions are in the same source file, you can declare var fabrics_json; at global scope and access it both with and without window. prefix.

Sign up to request clarification or add additional context in comments.

5 Comments

Actually the problem have no relation to global variables. It's a typical problem with async calls.
@hindmost I disagree. You can do async in a different way, however, the question is about setting and getting a global variable.
@hindmost This is no where related to async calls.
@Maxim Egorushkin The quote from OP: fabrics_json remains undefined either if I declare it in the beginning _of the script or not.
The actual problem is that the OP calls getFabrics in sync way and expects that AJAX response is available immediately after that call.
0

I wouldn't use a global variable here... There's no need. You just want a simple callback function.

//Pass in a callback function..
function getFabrics(callback){
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "af_general.php?action=fabrics", true);
    xhttp.send();
    xhttp.onreadystatechange = function() {
        if (this.readyState === 4 && this.status === 200) {
            callback(JSON.parse(this.responseText));
        }
    };
}


function addrow(){
    // pass in your code to getFabrics as a function to be executed on the callback.
    getFabrics(function(fabrics_json){
        var table=document.getElementById('stoixeia');
        var rows = table.getElementsByTagName("tr").length;
        var row = table.insertRow(-1);
        var cell1 = row.insertCell(-1);
        var rowno=rows-1;
        var pcs_ent=document.createElement('input');
        setAttributes(pcs_ent,{'type':'number','id':'pcs.'+rowno,'name':'pcs','style':'width: 4em;text-align:right','min':'0','max':'99','autocomplete':'off'});
        cell1.innerHTML='#'+rowno;
        cell1.appendChild(pcs_ent);
        var cell2 = row.insertCell(-1);
        var fabric_ent=document.createElement('select');
        console.log(fabrics_json);//returns undefined
        for (f in fabrics_json){
            option = document.createElement('option');
            option.value=fabrics_json[f][0];
            option.text=fabrics_json[f][1];
            fabric_ent.add(option);
        }
        //and goes on...
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.