-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.js
97 lines (97 loc) · 2.63 KB
/
load.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var count = 0;
var flag = true;
var map = {};
map[0]=true;
$(function () {
function escapeHtml(text) {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function show_message() {
$.ajax({
url: 'show_message.php',
type: 'POST',
data:{number_div: count, hidden: $('#block').val()},
dataType: 'json',
success: function (result) {
if(result && flag) {
flag=false;
$("#message_box").html(function (index, oldhtml) {
result.forEach(function (mes){
if(!map[mes.id]) {
if(mes.name == $("#my_name").val()) {
oldhtml += '<div class="my_name">' + mes.name + ' ' + mes.time +
'</div><div style="text-align: right" class="message"><p>' + escapeHtml(mes.message) + '</p></div>';
} else {
oldhtml += '<div class="name">' + mes.name + ' ' + mes.time + '</div><div class="message"><p>'
+ escapeHtml(mes.message) + '</p></div>';
}
map[mes.id]=true;
}
});
return oldhtml;
});
count += result.length;
document.getElementById('message_box').scrollTop = 9999*count;
flag=true;
}
}
});
}
function add_message () {
if(map[count]) {
var mes = $('#mes').val();
$('#mes').val('');
count += 1;
map[count] = true;
}
$.ajax(
{
url: 'add_message.php',
type: 'POST',
data:{msgtext: mes, hidden: $('#block').val()},
dataType: 'json',
success: function (mes) {
console.log(mes);
$("#message_box").html(function (index, oldhtml) {
if(mes.name == $("#my_name").val()) {
oldhtml += '<div class="my_name">' + mes.name + ' ' + mes.time +
'</div><div style="text-align: right" class="message"><p>' + escapeHtml(mes.message) + '</p></div>';
} else {
oldhtml += '<div class="name">' + mes.name + ' ' + mes.time + '</div><div class="message"><p>'
+ escapeHtml(mes.message) + '</p></div>';
}
return oldhtml;
});
document.getElementById('message_box').scrollTop = 9999*count;
}
});
};
$('#mes').keydown(function(event) {
if (event.which == 13 && !event.shiftKey) {
event.which = 0;
event.preventDefault();
add_message();
}
});
$("#log").click(function() {
if($('#login').val() == '')
alert('Введите свой ник и пароль, пожалуйста.');
});
$("#send").click(function() {
if($('#login').length>0 && $('#login').val() == '')
alert('Вы не залогинены.');
else
add_message();
});
function load() {
setInterval(function() {
show_message();
}, 2000);
}
load();
});