I'm trying to achieve sth like a facebook notifications '(1)' in the tab title. Below is a simplified code of my future's site message box - what I want to do is to use AJAX to check if the new message was added (new tr), but without refreshing the site, and then show expected number in tab title. Here is my code so far - but I have to refresh the site after adding a new table row...
<body>
<div id="container">
<div id="tab">
<table id="tabl">
<tr>
<th>ID</th>
<th>Name</th>
<th>Message</th>
</tr>
<tr>
<td>1</td>
<td>John</td>
<td>Hey! What's up!</td>
</tr>
<tr>
<td>2</td>
<td>Kylie</td>
<td>Are you there Kylie?</td>
</tr>
<tr>
<td>3</td>
<td>Peter</td>
<td>Please, buy me a milk</td>
</tr>
</table>
</div>
</div>
<script>
var count = document.getElementById('tabl').rows.length - 1;
var x = new XMLHttpRequest();
var i = 0;
x.onreadystatechange = function() {
if (x.readyState == 4 && x.status == 200) {
document.title = '(' + count + ')' + ' ' + document.title;
}
};
x.open('GET', 'http://localhost/php13/notif.php', true);
x.send();
</script>
</body>
How to achieve that without refreshing the site ?