I'm writing a script that basically loads a database as a table. This is just the barebones version of it, but should cover everything I'm trying to do.
HTML:
<a href="#" onclick="kiloseqResult=dbload('sample_kiloseq')">Load database</a>
<div id="result_table" style="visibility:hidden;">
<center>
<table border="0" cellspacing="3" cellpadding="3" id="summaryTable" class="table table-striped tablesorter">
<thead>
<tr style="font-weight: bold; text-align: center;">
<th>Well ID</th>
<th>Dominant Gene</th>
<th>%</th>
<th>Secondary Gene</th>
<th>%</th>
<th>No. of Reads that Mapped</th>
<th>No. of Mutations</th>
<th>Mutation Information</th>
<th>View</th>
</tr>
</thead>
<tbody id="summaryBody">
</tbody>
</table>
</div>
Javascript:
var kiloseqResult
function dbload(name){
var r = new XMLHttpRequest();
r.open("GET", "/db/"+name, true);
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
kiloseqResult = r.responseText;
console.log(kiloseqResult)
return kiloseqResult
structureTable();
};
r.send()
}
function structureTable(){
if (kiloseqResult==null){
throw "Error: no databse defined"
};
document.getElementById("summaryTable").style.visibility="visible";
kiloseqDatabase = JSON.parse(kiloseqResult);
var table = document.getElementById("summaryBody");
for (i=0;i<kiloseqDatabase.length;i++){
var row = table.insertRow(i);
var cell = row.insertCell(0);
cell.innerHTML = "Some HTML here"
};
}
The AJAX request works, and I've confirmed this, so there is a result loading in the var kiloseqResult (and I've declared the variable in multiple locations to makes sure it's being loaded). However structureTable() isn't being called when dbload() finishes, and I can't seem to figure out why.
Any help would be much appreciated.