62,073
社区成员
发帖
与我相关
我的任务
分享
$(function () {
timestamp = 0;
updateMsg();
$("#chatform").submit(function () {
$.post("chatroom.aspx", {
message: $("#msg").val(),
name: $("#author").val(),
action: "postmsg",
time: timestamp
}, function (xml) {
$("#msg").val("");
addMessages(xml);
});
return false;
});
function addMessages(xml) {
if ($("status", xml).text() == "2") return;
timestamp = $("time", xml).text();
$("message", xml).each(function () {
var author = $("author", this).text();
var content = $("text", this).text();
var htmlcode = "<strong>" + author + "</strong>:" + content + "<br/>";
$("#messagewindow").prepend(htmlcode);
});
};
function updateMsg() {
$.post("chatroom.aspx", { time: timestamp }, function (xml) {
$("#loading").remove();
addMessages(xml);
});
setTimeout('updateMsg()', 4000);
}
})
protected void Page_Load(object sender, EventArgs e)
{
string header = Request.Headers["X-Requested-With"];
if (!string.IsNullOrEmpty(header) && header.ToLower().Equals("xmlhttprequest"))
{
Response.Clear();
Response.Write(string.Format("<response><user></user><time></time></response>", Request["user"], DateTime.Now));
Response.End();
}
}