C. Kelly C. Kelly - 5 months ago 15
jQuery Question

NAN Errors when breaking a single page site into Multi page site

Problem

I have built a single page site that runs a bunch of calculations in JavaScript,
taking input values and/or user input values and returning the sums to it's designated input(for the sum output). However after the customer has viewed the UI they decided that it made sense to break the single page site into different pages.

My Effort

I thought as long as I include the script tags to the new pages all I would have to do is cut the portions out of the single page and paste it in the new page it should work right? Well, I ran the page in the browser and I get NAN errors, I have looked for a solution online but have not found one. any help would be appreciated

Typical HTML Input

<tr>
<td class="input_gray">0.00015</td>
<td><input class="input_green custom_Default" id="pipeRoughness" type="number" data-value="0.00015" value="0.00015">
</td>
<td>Feet</td>
<td>Pipe Roughness</td>
</tr>


Typical JavaScript Function

var eD = function() {
var val1 = siDin();
var val2 = parseFloat($('#pipeRoughness').val());
var val3 = val2 / 3.28;
var total = val3 / val1;
$('#eDCalc').val(total);
return total;
};
eD();


Order of my Script tags

<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script src="js/calc.js"></script>
<script src="js/manualEntryCalculations.js"></script>
<script src="js/classie.js"></script>

Answer

I suspect the problem is with this line: var val1 = siDin();

When you split the JavaScript into multiple files, it is possible that the siDin() function was moved out of scope. You can test this by pasting that function just above the var eD = function() { line and seeing if the call resolves.

To fix this, make sure that whichever file contains function siDin(){} is included before the file containing var eD = function().

Also you'll need to make sure siDin has global scope (which is the easiest, but probably not the best, way of making sure the function is visible to the caller).