简体   繁体   中英

return value to function within a function

Trying to get a return value from getUrl function but it comes back as undefined.
I would appreciate any help.

Thanks
Here is the code:

function createXmlFicaRsi(xmlDoc,xmlFileName) {     
    var mystr = "<?xml version='1.0' encoding='utf-8'?><result><rows>"+strStor+"</rows></result>"
    jQuery(document).ready(function(){ 
      jQuery("#fRsiGrid").jqGrid({
        datatype: 'xmlstring',
        datastr : mystr,
        colNames:['Year','Earnings', 'Amt<br/>Needed <br/>1 QC','Amt<br/>Needed <br/>4 QC','#<br/>of<br/> QCs','Monthly<br/>Under FRA','Yearly<br/>Under FRA','Monthly<br/> Yearly of<br/> Attain.<br/> FRA','Year of<br/> Attain. of<br/> FRA','YOC*','Sum of<br/>Post-1977<br/>YOCs'],
        colModel :[ 
            {name:'yearRsi', index:'yearRsi', width:55, resizable:false, align:'center', sorttype:'int'},
            {name:'earnRsi', index:'earnRsi', width:65, resizable:false, align:'right', sortable:false}, 
            {name:'1qcRsi', index:'1qcRsi', width:65, resizable:false, align:'right', sortable:false}, 
            {name:'4qcRsi', index:'4qcRsi', width:65, resizable:false, align:'right', sortable:false}, 
            {name:'numqcRsi', index:'numqcRsi', width:40, resizable:false, align:'right', sortable:false}, 
            {name:'mfra', index:'mfra', width:65, resizable:false, align:'right', sortable:false}, 
            {name:'yfra', index:'yfra', width:65, resizable:false, align:'right', sortable:false},
            {name:'myafra', index:'myafra', width:85, resizable:false, align:'right', sortable:false},
            {name:'yafra', index:'yafra', width:65, resizable:false, align:'right', sortable:false},
            {name:'yoc', index:'yoc', width:65, resizable:false, align:'right', sortable:false},          
            {name:'sumpost', index:'sumpost', width:60, resizable:false, align:'right', sortable:false} ],     
        rowNum:-1,      
        hidegrid: false,
        width: 760, 
        height: 460,
        shrinkToFit: false,         
        caption: '<span id=fRsiGrid_caption>FICA Earnings, QC, AET and YOC amounts after 1977</span>'       
      });     

      $('.ui-jqgrid .ui-th-column').css('height', '40px');
      $('.ui-jqgrid .ui-jqgrid-htable th div').css('height', '40px'); 
      $('.ui-jqgrid-title').css('font-size', '.8em');//Font size for title
      $('.ui-jqgrid .ui-th-column').css('font-size', '.7em');//Font size for header content 
      $('#fRsiGrid_caption').append("<span id='whatLink' style='font-size:large;color:blue;text-decoration:none;cursor:pointer'>*</span>");     

    }); 
    $('#jqgh_1qcRsi').addClass("gridLink");
    $('#jqgh_4qcRsi').addClass("gridLink");
    $('#jqgh_mfra').addClass("gridLink");
    $('#jqgh_yfra').addClass("gridLink");
    $('#jqgh_myafra').addClass("gridLink");
    $('#jqgh_yafra').addClass("gridLink");
    $('#jqgh_yoc').addClass("gridLink");

    $("#jqgh_1qcRsi").click(function() {
        var nurl = getUrl("QueryView-QC");
        alert(nurl);        
    }); 
}

function getUrl(urlNm){
    DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getUrls', urlNm, doQueryResults);
    function doQueryResults(r){     
        xmlDoc = loadXMLString(r);      
        y = xmlDoc.getElementsByTagName("URL");

        for (i = 0; i < y.length; i++) {            
            url = y[i].attributes.getNamedItem("val").nodeValue;            
            if (url == urlNm)
            {                           
                url = y[i].childNodes[0];
                //alert(url.nodeValue);
                url = url.nodeValue;
                return url;
            }           
        }   
    }
}   

AJAX is asynchronous

It appears as though you're making an AJAX request inside getURL .

Remember, AJAX requests are asynchronous by default. That means when you call getURL , the code after it executes immediately instead of waiting for the AJAX response.

Any code that relies on the response needs to be done in the callback to the AJAX request.

It appears as though your callback function is doQueryResults . Place the alert() in that function, and it should fire.

The inner functions are not activated, so running testOne() doesn't do anything. If you want to run it, use:

  function testOne() {
   (function inside() {
      //some other code
      (function anotherOne() {
          var nurl = getUrl("firstLink");
          alert(nurl);
      })();
   })()
  }
  testOne();

Or even

function testOne() {
   //some other ocde
   return function inside() {
      //some other code
       return function anotherOne() {
          var nurl = getUrl("firstLink");
          alert(nurl);
      };
   }
}
testOne()()();

but may be you should ask yourself: what's the use? :D

Appears to work for me. See my Fiddle: http://jsfiddle.net/nBTLB/

there is no problem with that you function is defined within another one

but it seems that getUrl is defined after testOne maybe it hasn't been defined by the time anotherOne gets called?

The only way you can get undefined is to call getUrl with an argument other than "firstLink". Furthermore, the method getUrl should have an else statement with an alternative return when the provided argument is not "firstLink"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM