0

I need to use variable generated value from first function (variable url), in second function, here is a code:

var kategorije = ["c1", "c2", "c3", "c4", "c5", "c6"];
var url;

function checkbox_test() {  //first function
// calculating url ...
alert(url); // here everything is ok, something like c1=x&c2=a&c3=5 ...
}

function loadXMLDoc()   {  // second function
   regid = "123456abcdefg";
   var xmlhttp;
   xmlhttp=new XMLHttpRequest()
   xmlhttp.onreadystatechange=function()    {
     if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            }
            }
     xmlhttp.open("GET","http://www.example.com/?regId=" + regid + '&' + url, true);
     xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
     xmlhttp.send();
   alert(url); // just checking is value of url is correct! here it say url is undefined!
 }

I need to pass value of url generated in first function in GET as url. Thanks!

bladimir
  • 15
  • 6
  • First: `checkbox_test()` have to called before `loadXMLDoc()`; Second: `checkbox_test()` can't be async, or the value would not be set at the time `loadXMLDoc()` reads it. – DontVoteMeDown Apr 23 '15 at 17:16

2 Answers2

0

You have to call checkbox_test() before your second function, and when you do assign var url to the output of checkbox_test() after returning the url:

var kategorije = ["c1", "c2", "c3", "c4", "c5", "c6"];
var url;

function checkbox_test() {  //first function
// calculating url ...
alert(url); // here everything is ok, something like c1=x&c2=a&c3=5 ...
return url
}

url = checkbox_test();
loadXMLDoc(url);
function loadXMLDoc(url)   {  // second function
   regid = "123456abcdefg";
   var xmlhttp;
   xmlhttp=new XMLHttpRequest()
   xmlhttp.onreadystatechange=function()    {
     if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            }
            }
     xmlhttp.open("GET","http://www.example.com/?regId=" + regid + '&' + url, true);
     xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
     xmlhttp.send();
   alert(url); // just checking is value of url is correct! here it say url is undefined!
 }
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • not work! in my html I have checkboxes with SEND button which fires function checkbox_test(), and if I call like url=checkbox_test() before second function I receive: alert that no checkbox are checked. – bladimir Apr 23 '15 at 19:09
0

In addition to the suggestions above, you could try using 'callbacks', a thread here has some good contributions and examples.

So in your context, you may use a callback like this,

function checkbox_test( callback ) {  
  // do calculation and when 'url' is available, 
  // invoke callback (which is loadXMLDoc())
  callback( url );

}

function loadXMLDoc( aValuePassedFrom_checkbox_test ) {
   // do stuffs with value (url)
}

// call function checkbox_test, passing in callback fn
checkbox_test( loadXMLDoc );

Hope this helps.

Community
  • 1
  • 1
daxeh
  • 1,083
  • 8
  • 12