简体   繁体   English

JavaScript错误:未捕获的SyntaxError:意外令牌}

[英]Javascript error: Uncaught SyntaxError: Unexpected token }

I am creating a chat for my site. 我正在为我的站点创建聊天。 It works fine, but a function isn't working inside of a HTML button. 它可以正常工作,但是HTML按钮内部没有功能。 I have 3 files I use for it, and one line isn't working. 我有3个文件用于其中,并且其中一行无效。

Before, I had an issue where I set the Javascript function, create the button, and it would say the function doesn't exist. 以前,我遇到一个问题,需要设置Javascript函数,创建按钮,然后说该函数不存在。 I fixed that, by putting the function below the buttons. 我通过将功能放在按钮下方来解决此问题。

Each button shows an online member, who is on your friends list. 每个按钮显示一个在线成员,该成员在您的朋友列表中。

The error I get in confusing me, as I have no idea how this is being caused. 我弄错了我的错误,因为我不知道这是怎么引起的。

Error: 'Uncaught SyntaxError: Unexpected token } users.php:3' 错误:“未捕获的语法错误:意外的令牌} users.php:3”

(users.php is the page I was on, if I change it that changes to the page name.) (users.php是我所在的页面,如果我将其更改为页面名称,则为该页面。)

With chrome, I can click on the error, that brings up this: 'window.script1357688207590=1;' 使用chrome,我可以单击错误,显示以下错误:'window.script1357688207590 = 1;'

Scripts (sorry for being so long): 脚本(很久很抱歉):

Script 1: (main script): 脚本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);

Script 2 (function script): 脚本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 script (bchat.php): 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';
    }
?>

Try to change the php to add an id attribute to the button, or class if there is going to be more than one. 尝试更改php以将id属性添加到按钮,或者如果要添加多个,则添加class。 Also, remove the onclick attribute 另外,删除onclick属性

<button class="btnChat" ...

Just realized you will need to add in the parameters as well. 刚刚意识到,您还需要添加参数。 I would add those to data attributes on the button. 我将这些添加到按钮上的数据属性。

<button class="btnChat" data-url="$url" data-usern="$usern" 

You will need to make sure the $url and $usern are escaped properly 您需要确保$ url和$ usern正确转义

then in the script 2 file you can hook up the click event for that button: 然后在脚本2文件中,您可以连接该按钮的click事件:

$(function(){

  $('body').on('click','.btnChat',function(){
      chatWith($(this).data('url'),$(this).data('usern'));
  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM