I'm trying to develop something in real time which in this case, I'm using socketio for nodejs. Although everything else is working fine, my main issue now is that my program loads the javascript first before the HTML, which therefore means that the output at start is just empty.
In my code, the javascript calls everything else in the database and is concatenated by tags where in it is later thrown in the html.
Here is my code in html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<tr>
<td>
<table id="output" border=1>
<thead id="headers">
<th>Name</th>
<th>Address</th>
<th>Number</th>
</thead>
<tbody id="online">
</tbody>
</table>
</td>
<td>
<table id="output" border=1>
<thead id="headers">
<th>Name</th>
<th>Address</th>
<th>Number</th>
</thead>
<tbody id="offline">
</tbody>
</table>
</td>
</tr>
</table>
<script src="/chat.js"></script>
</body>
</html>
And here is the javascript:
var online = document.getElementById('online'),
offline = document.getElementById('offline');
socket.on('user joined', function(data) {
online.innerHTML = "";
for (var ctr = 0; ctr < data.length; ctr++) {
online.innerHTML += '<td>' + data[ctr].name + '</td>' +
'<td>' + data[ctr].address + '</td>' +
'<td>' + data[ctr].number + '</td>';
}
});
socket.on('user left', function(data) {
offline.innerHTML = "";
for (var ctr = 0; ctr < data.length; ctr++) {
offline.innerHTML += '<td>' + data[ctr].name + '</td>' +
'<td>' + data[ctr].address + '</td>' +
'<td>' + data[ctr].number + '</td>';
}
});
I've already searched this online however my case is different as I call the javascript file in the html, instead of having all the script with the html itself.
I've also read that having the javascript file be called after the html works, but apparently, I've done that (as seen in my codes) and still nothing.