我正在为我的网站创建一个聊天。它工作正常,但一个函数在 HTML 按钮内不起作用。我有 3 个用于它的文件,但有一行无法正常工作。
之前,我有一个问题,我设置 Javascript 函数,创建按钮,它会说该函数不存在。我通过将功能放在按钮下方来解决这个问题。
每个按钮都会显示一个在线成员,该成员在您的朋友列表中。
我让我感到困惑的错误,因为我不知道这是如何引起的。
错误:'Uncaught SyntaxError: Unexpected token } users.php:3'
(users.php 是我所在的页面,如果我更改它会更改页面名称。)
使用 chrome,我可以单击错误,这会显示:'window.script1357688207590=1;'
脚本(抱歉这么长):
脚本1:(主脚本):
var username = '[error]';
$.ajax({
url: 'bchat.php',
type: 'post',
data: { method: 'method2' },
success: function(data){
username = data;
}
});
$.ajax({
url: 'bchat.php',
type: 'post',
data: { method: 'method1' },
success: function(data){
if(data!='do not show chat'){
function ToggleChat(tof){
if(tof == true){
$('#chatWindow').css('visibility','visible');
}else{
$('#chatWindow').css('visibility','hidden');
}
}
$('body').append('\
<div id=chatWindow style="background:white; visibility
:hidden; position: fixed; bottom: 0; right: 0; margin-top:-30px; width:200px; height:350px;">\
<div style="width:100%; height:100%; border:solid black 1px;">\
<div style="hieght:20px; background:rgb(40,40,40); color:white;">\
'+data+'<span> </span>'+username+'\
<button id=chat_close style="float:right; background:rgba(0,0,0,0); border:none; color:white;">\
Close\
</button>\
</div>\
<div id=chatOnline style="overflow:auto;">\
</div>\
</div>\
</div>\
<div id=chat__window style="background:white; visibility:hidden; position: fixed; bottom: 0; right: 200; margin-top:-30px; margin-left:-400px; width:200px; height:350px;">\
\
</div>\
<div id=chat style="position: fixed; bottom: 0; right: 0;">\
<button id=chat_open style="border:none; padding:5px; color:whiteSmoke; background-image: -ms-linear-gradient(bottom, #4A4A4A 0%, #00070A 100%);background-image: -moz-linear-gradient(bottom, #4A4A4A 0%, #00070A 100%);background-image: -o-linear-gradient(bottom, #4A4A4A 0%, #00070A 100%);background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0, #4A4A4A), color-stop(1, #00070A));background-image: -webkit-linear-gradient(bottom, #4A4A4A 0%, #00070A 100%);background-image: linear-gradient(to top, #4A4A4A 0%, #00070A 100%); width:200px; height:30px;">\
'+data+' <span> </span>Chat\
</button>\
</div>');
$('#chat_open').click(function() {
ToggleChat(true);
});
$('#chat_close').click(function() {
ToggleChat(false);
});
}
}
});
//
//update online users
//
function updateOnline(){
$.ajax({
url: 'bchat.php',
type: 'post',
data: { method: 'method3' },
success: function(data){
$('#chatOnline').html(data);
}
});
}
updateOnline();
setInterval(updateOnline,5000);
脚本 2(功能脚本):
function chatWith(url,un){
$('#chat__window').html('<div style="width:100%; height:100%; margin-top:-30px; border:solid black 1px;"><div style="hieght:20px; background:rgb(40,40,40); color:white;">'+url+'<span> </span>'+un+'<button id=chat_chatting_close style="float:right; background:rgba(0,0,0,0); border:none; color:white;">Close</button></div><div id=chat_chats style="overflow:auto;"></div></div>');
$('#chat__window').css('visibility','visible');
}
PHP 脚本 (bchat.php):
<?php
include_once("./login_manager_php_file.php");
if($username&&$userid){
$method = $_POST['method'];
if($method){
if($method=='method1'){
$url = $images['logged_in'];
$html = "<img src='".$url."' width=7.5 height=7.5/>";
}elseif($method=='method2'){
echo $username;
}elseif($method=='method3'){
$friends_q = mysql_query("SELECT * FROM friends WHERE `with`='$username' OR `friender`='$username'");
$thtml = '';
if($friends_q){
while($friend = mysql_fetch_assoc($friends_q)){
if($friend['with']==$username){
$usern = $friend['friender'];
}else{
$usern = $friend['with'];
}
$url = '';
if(ifLoggedIn($usern)===true){
$url = $images['logged_in'];
}else{
$url = $images['logged_out'];
}
if(ifLoggedIn($usern)===true && $usern != $username){
$on = $url;
$html = "
<button onClick='
chatWith('$url','$usern');
'
style='border:none; background:rgba(0,0,0,0);'><img src='".$url."' width=7.5 height=7.5/>";
$thtml = $thtml.$html.$usern.'</button><br/>';
}
}
}
echo $thtml;
}elseif($method==='method4'){
if($_POST['usrn']){
$url = '';
if(ifLoggedIn($_POST['usrn'])===true){
$url = $images['logged_in'];
}else{
$url = $images['logged_out'];
}
echo "<img src='".$url."' width=7.5 height=7.5/>";
}else{
echo '[error]';
}
}
}
}else{
echo 'do not show chat';
}
?>