troubleshooting Question

Why does Javascript function run without being called??

Avatar of swendell
swendell asked on
JavaScript* functions
10 Comments2 Solutions597 ViewsLast Modified:
Why does Javascript function DateDifff run without being called??
Even more interesting to me is that unassigned variables are throwing errors that are inside logic blocks that should not run (since the logic condition is not true)
So how is this running? Even if I did something to cause it to run; why it is executing logic with IF statement which is not true?

Are all variables checked for values when a page is loaded? Seems odd...Error
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<head>
    <script type="text/javascript">
        $(window).on('load', function() //Runs After page loads. We want to compute Column totals
            // We get a cell's value/text(html) based upon id of column and row
        {
            var Column1Tot = 0 ;
            // Lets start totaling Scenerio1
            var column = $('#Scenerio2').index(); // This is the column # (index/screnerio). this ID is found in the <col> tag
                                                  // When a column can NOT be found it seems to use the last column
            var cell = $('#RecordingFees').find('td').eq(column); // Here we get the row whose ID is found in <td> tag and a coulm for that row found directly above
            //alert( "rECORDINGfEESXXX = " +  parseFloat(cell.html())   ); // If row ID does not exist , we get Nan error
            Column1Tot +=  parseFloat(cell.html());
            var cell = $('#Commissions').find('td').eq(column);
            //alert(parseFloat(cell.html())   );
            Column1Tot +=  parseFloat(cell.html()); // Take previous Column1Tot value and add to it using += operand
            //alert("Column Total " + Column1Tot);

        });
    </script>

    <script type="text/javascript">
    function datedifff(type, scenerio) // scenerio # is 1,2 or 3. type is "RETax"
    {
        var RETaxPaidThru = new Date();

        if (scenerio === 1 && type === "RETax") {
            RETaxYearPaidThru =  <?php echo $_POST['PTaxYear1']; ?>;
            ClosingDate = new Date("<?php echo $_POST['closingdate1']; ?>"); //quotes required around text date
            // Seperatley assigning date text to date field did not work, it seemed to convert the date field back to a text field so above I created and assigned date in one step.
        }
        else if (scenerio === 2 && type === "RETax"){
            RETaxYearPaidThru =  <?php echo $_POST['PTaxYear2']; ?>;
            ClosingDate = new Date("<?php echo $_POST['closingdate2']; ?>");
        }
        else if (scenerio === 3 && type === "RETax"){
        RETaxYearPaidThru =  <?php echo $_POST['PTaxYear3']; ?>;
        ClosingDate = new Date("<?php echo $_POST['closingdate3']; ?>");
        }

        RETaxPaidThru.setMonth(11); //Month is base 0, so DEC = 11 !
        RETaxPaidThru.setDate(31);
        RETaxPaidThru.setFullYear(RETaxYearPaidThru);

        // The number of milliseconds in one day
        var ONE_DAY = 1000 * 60 * 60 * 24;

        // Convert both dates to milliseconds
        var RETaxPaidThru_ms = RETaxPaidThru.getTime();
        var ClosingDate_ms = ClosingDate.getTime();

        // Calculate the difference in milliseconds
        var difference_ms = Math.abs(RETaxPaidThru_ms - ClosingDate_ms);

        // Convert back to days and return
        return Math.round(difference_ms/ONE_DAY);

    }
    </script>
SOLUTION
Olaf Doschke
Software Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros