I currently have a large amount of JavaScript code within my HTML file (1500 lines), and I am looking to relocate some portions of it to another JS file. My goal is to create a single file for importing data, and I am exploring the best approach to achieve this.
Here is an excerpt from my gameDataImport.js file:
function gameImport(gameID){
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","../riskmanagment/Data/gameData.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var numberOfWorkers;
var x=xmlDoc.getElementsByTagName("game");
for (i=0;i<x.length;i++)
{
if(x[i].getAttribute('id') == gameID)
{
// Assigning values to various variables from XML data
}
}
As I attempt to access the data, here is how I've structured it:
In the head section:
<script type="text/javascript" src="js/gameDataImport.js"></script>
In the body section, inside a script tag:
numberOfWorkers = gameImport(gameID).numberOfWorkers;
However, I am encountering the following error:
Uncaught TypeError: Cannot read property 'numberOfWorkers' of undefined
While I have only attempted to fetch one variable so far, I plan to explore additional options moving forward.