by nolovelust
29. April 2010 15:15
Here is the simple JavaScript code to alter DOM with jQuery and add Ajax request result to the first place in a ul.
First add latest JQuery reference to your page and create Ajax response page. In my case it is live.ashx which outputs data like below
<li>li1</li>
<li>li 2</li>
Than create an ul in your page like below
<ul id="LiveTraffic"></ul>
Then add below JavaScript and you are good to go
var lastdata = null;
var liCount = 0;
$(document).ready(function () {
$.get('live.ashx?' + Math.random(), function (data) {
if (lastdata != data) {
$("#LiveTraffic").prepend(data).slideDown('slow');
lastdata = data;
liCount += 1;
}
})
if (liCount > 10) {
$('#LiveTraffic li:not(:first)').remove();
liCount = 0;
}
setTimeout(arguments.callee, 10000);
});
2f50e47e-79d6-4e6a-868c-e2c9059ca6ce|1|4.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
Tags: jQery, Ajax, li, ul
Useful