0

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.

Leto
  • 503
  • 3
  • 18
Gina
  • 17
  • 5
  • 3
    Because you're calling it after the `return`? –  Oct 30 '14 at 19:59
  • Not directly related to your problem, but your `
    ` element (which you should not be using anyway) is not close.
    –  Oct 30 '14 at 20:04
  • I didn't realize I couldn't call it after the return. THANK YOU. Also, yes, the
    shouldn't be there either. Much appreciated :)
    – Gina Oct 30 '14 at 20:07

1 Answers1

2

Once a return statement is hit, javascript will stop processing the rest of your function, so any lines after your return will be ignored. So, switch the order of these, so:

    structureTable();
    return kiloseqResult
KJ Price
  • 5,774
  • 3
  • 21
  • 34