nad nad - 2 months ago 11
ASP.NET (C#) Question

Javascript function is not working as expected

I have a function which is not working is expected.

The problem is that, I have set

if (Trim(ObjPriXMLHTTP.responseText) != 0)
then it should go inside and run another function, which is not going currently when my
if (Trim(ObjPriXMLHTTP.responseText) != 0)
==
1
.

The debugger just throws me out of the parent function.
I want that to run when value is other than
0


Below is my js function

function getCounterForCheck() {
StrPriFnName = "getCounterForCheckInward(" + document.getElementById('TxtInwardNo').value + ")";
var ObjPriXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP")
ObjPriXMLHTTP.open("GET", "FrmInwardXMLHTTP.aspx?para=" + StrPriFnName, false);
ObjPriXMLHTTP.send("");

if (Trim(ObjPriXMLHTTP.responseText) != 0) {

function getOtherDBInward() {
StrPriFnName = "FunGetOTHERDBInward(" + document.getElementById('TxtInwardNo').value + ")";
var ObjPriXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP")
ObjPriXMLHTTP.open("GET", "FrmInwardXMLHTTP.aspx?para=" + StrPriFnName, false);
ObjPriXMLHTTP.send("");

if (Trim(ObjPriXMLHTTP.responseText) != "") {
var StrPriData = ObjPriXMLHTTP.responseText.split('~');
document.getElementById('HidRefMkey').value = StrPriData[0];
document.getElementById('TxtDeliveredBy').value = StrPriData[1];
document.getElementById('cmbRecdDept').value = StrPriData[2];
FunEmpFillDept();
document.getElementById('cmbRecdEmp').value = StrPriData[3];
document.getElementById('HidCurrentEmp').value = StrPriData[3];
document.getElementById('Txt_RefBillNo').value = StrPriData[6];
igdrp_getComboById("DtmInfRef_DocDate").setValue(Trim(StrPriData[7]));
igedit_getById("TxtWN_Billamt").setValue(StrPriData[8]);
document.getElementById('TxtRemarks').value = StrPriData[9];
document.getElementById('TxtPartyName').value = StrPriData[10];
}

else {
alert("ERROR: Document does not exist");
//alert("ERROR: Document does not exist and status also");
document.getElementById('TxtInwardNo').focus();
return false;
}
}

}
else {
//alert('invoice not found');
}
}

Answer

There are a couple of issues with that code.

  1. Until ES2015, it was invalid to declare a function inside a flow-control block (like the body of an if), and it remains a Really Bad Idea™. But that's what your code is doing.

  2. You've declared the function, but you haven't called it. So stepping right out is exactly what the debugger should do; there was nothing left to do in getCounterForCheck.

  3. In comments, you've said Trim(ObjPriXMLHTTP.responseText) will return "1" or "0" (e.g., strings). But you're comparing that to 0 (the number). Since you're using loose comparison (!=), JavaScript will coerce the value for you, and in this particular case it will do so in the way you're probably expecting. I raise it just because it might make sense for Trim to explicitly convert the value and return 1 or 0 (e.g., as numbers).

Declare the function outside the if block, and call it if you want to call it. Something like:

function getCounterForCheck() {
    StrPriFnName = "getCounterForCheckInward(" + document.getElementById('TxtInwardNo').value + ")";
    var ObjPriXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP")
    ObjPriXMLHTTP.open("GET", "FrmInwardXMLHTTP.aspx?para=" + StrPriFnName, false);
    ObjPriXMLHTTP.send("");

    if (Trim(ObjPriXMLHTTP.responseText) != 0) {
        getOtherDBInward();
    }
    else {
        //alert('invoice not found');
    }

    function getOtherDBInward() {
        StrPriFnName = "FunGetOTHERDBInward(" + document.getElementById('TxtInwardNo').value + ")";
        var ObjPriXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP")
        ObjPriXMLHTTP.open("GET", "FrmInwardXMLHTTP.aspx?para=" + StrPriFnName, false);
        ObjPriXMLHTTP.send("");

        if (Trim(ObjPriXMLHTTP.responseText) != "") {
            var StrPriData = ObjPriXMLHTTP.responseText.split('~');
            document.getElementById('HidRefMkey').value = StrPriData[0];
            document.getElementById('TxtDeliveredBy').value = StrPriData[1];
            document.getElementById('cmbRecdDept').value = StrPriData[2];
            FunEmpFillDept();
            document.getElementById('cmbRecdEmp').value = StrPriData[3];
            document.getElementById('HidCurrentEmp').value = StrPriData[3];
            document.getElementById('Txt_RefBillNo').value = StrPriData[6];
            igdrp_getComboById("DtmInfRef_DocDate").setValue(Trim(StrPriData[7]));
            igedit_getById("TxtWN_Billamt").setValue(StrPriData[8]);
            document.getElementById('TxtRemarks').value = StrPriData[9];
            document.getElementById('TxtPartyName').value = StrPriData[10];
        }

        else {
            alert("ERROR: Document does not exist");
            //alert("ERROR: Document does not exist and status also");
            document.getElementById('TxtInwardNo').focus();
            return false;
        }
    }
}

I haven't looked closely at the function, but if it doesn't have to be nested inside getCounterForCheck, you might move it out.