简体   繁体   中英

javascript function return “undefine” from jsp page

i have a jsp page and call a JS function which is in some abc.js file from this JSP page. i have included this js file to jsp page.

JSP JavaScript Code:-

function doFinish(tableId, col, field)
{
   var oldselectedCells = "";
   var selItemHandle = "";
   var selRightItemHandle = "";
   var left = -1;
   var right = -1;
   // Get the table (tBody) section
   var tBody = document.getElementById(tableId);
   // get field in which selected columns are stored
   var selectedCellsFld = document.getElementById(tableId + datatableSelectedCells);

   selectedCellsFld.value = oldselectedCells;
   for (var r = 0; r < tBody.rows.length; r++)
   {
      var row = tBody.rows[r];
      if (row.cells[col].childNodes[0].checked == true)
      {
         selectedCellsFld.value = oldselectedCells +
                                 row.cells[col].childNodes[0].id;
         selItemHandle = row.cells[col].childNodes[0].value

         oldselectedCells =  selectedCellsFld.value + datatableOnLoadDivider;

         left = selItemHandle.indexOf("=");
         right = selItemHandle.length;
         selRightItemHandle = selItemHandle.substring(left+1,right);
         var index=getColumnIndex(tBody,"Name");
         if(index!=null)
         {
            if(field == 1)
            {
               window.opener.document.TemplateForm.eds_asbactionscfg_item_handle_child_physpart.value = selRightItemHandle;
               window.opener.document.TemplateForm.ChildPhysicalPart.value = row.cells[index].childNodes[0].innerHTML;
            }
            else if (field == 2)
            {
               window.opener.document.TemplateForm.eds_asbactionscfg_dev_doc_item_handle_name.value = selRightItemHandle;
               window.opener.document.TemplateForm.DeviationObject.value = row.cells[index].childNodes[0].innerHTML;
            }
            else if (field == 3)
            {
               window.opener.document.TemplateForm.eds_asbactionscfg_dev_doc_item_handle_name.value = selRightItemHandle;
               window.opener.document.TemplateForm.DeviationObject.value = row.cells[index].childNodes[0].innerHTML;
            }
         }

      }
   }

   window.close();
}

JS Code:-

function getColumnIndex(tBody,columnName)
 {
    var cells = tBody.parentNode.getElementsByTagName('th');     
    for (var i=0;i<cells.length; i++)
    {
       if(cells[i].hasChildNodes())
       {
          if(cells[i].childNodes[0].innerHTML.replace(/(\r\n|\n|\r)/gm ,"").trim() == columnName)
          {
             return i;
          }
       }
    }
 }

i had debug this code with firebug & calling getColumnIndex(tBody,columnName) function works fine but when it return to caller the var index=getColumnIndex(tBody,"Name"); the index value is "undefine".

suggest some solution.

getColumnIndex(tBody,columnName) function works fine. as if it matches this if condition

 if(cells[i].childNodes[0].innerHTML.replace(/(\r\n|\n|\r)/gm ,"").trim() == columnName)
          {
             return i;
          }

so that it returns something.

but when you replace this var index=getColumnIndex(tBody,"Name"); so that coulmnName would be "Name" in String. And it doesn't match with any columnName so that your condition going to be wrong and function doesn't return anything.

var index=getColumnIndex(tBody,"Name"); the index value is "undefine".

suggestion is put some else condition on that and return some error message like this :

 if(cells[i].childNodes[0].innerHTML.replace(/(\r\n|\n|\r)/gm ,"").trim() == columnName)
          {
             return i;
          } else{
       // put some error message
       // return null   
}

i had debug this code with firebug & calling getColumnIndex(tBody,columnName) function works fine

From this, I'm assuming that there isn't anything wrong with the implementation of your getColumnIndex function, so your issue with getting an undefined value must have to do with when this function is returning a value.

but when it return to caller the var index=getColumnIndex(tBody,"Name"); the index value is "undefine".

This leads me to assume that your tBody variable is not being set correctly, given that the "function works fine".

I'm assuming there is a case in your code where the conditions of your getColumnIndex function is not met.

function getColumnIndex(tBody,columnName)
 {
    var cells = tBody.parentNode.getElementsByTagName('th');     
    for (var i=0;i<cells.length; i++)
    {
       if(cells[i].hasChildNodes())
       {
          if(cells[i].childNodes[0].innerHTML.replace(/(\r\n|\n|\r)/gm ,"").trim() == columnName)
          {
             return i;
          }
       }
    }
    // If your code reaches this point, then the prior conditions have not been met
    // You can choose to do something else here for return false/undefined etc.
    return undefined;
 }

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