【问题标题】:Javascript error: Uncaught SyntaxError: Unexpected token }Javascript 错误:未捕获的 SyntaxError:意外的令牌}
【发布时间】:2017-10-16 11:07:07
【问题描述】:

我正在为我的网站创建聊天。它工作正常,但一个函数在 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';
    }
?>

【问题讨论】:

  • 一切正常,但在线用户按钮上的功能,其中 onClick='chatWith();'
  • 您可能需要检查您的 PHP 代码,以确保您不会在 select 语句中面临 SQL 注入的风险,因为您没有清理或检查来自客户端的输入。用户名“O'Rielly”(或更糟!)会怎样?

标签: php javascript jquery html


【解决方案1】:

尝试更改 php 以将 id 属性添加到按钮或类(如果有多个)。另外,删除 onclick 属性

<button class="btnChat" ...

刚刚意识到您还需要添加参数。我会将这些添加到按钮上的数据属性中。

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

您需要确保 $url 和 $usern 正确转义

然后在脚本 2 文件中,您可以连接该按钮的点击事件:

$(function(){

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

});

【讨论】:

  • 好吧,我做不到。 PHP 为 javascript 函数获取了一些信息。再说一次,我可以使用 ajax 来获取该函数内部的信息。我会试试的。
  • 我的点击功能有所不同。我可以做 $('.btnChat').click(chatWith(something,something)); ?
  • 它(排序)工作,我没有收到错误,但它没有正确地将它提交给 chatWith 函数。我仔细检查并将 abc 登录到控制台,在函数中,单击它,它没有记录它。
  • 当然,我忘记了事件连接时按钮不在页面上..我只是对答案进行了更改,这样事件就会触发。
  • 谢谢,它现在可以工作了,.click(function(){ });不工作?因为以前就是这样。
猜你喜欢
  • 2016-10-23
  • 2014-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 2015-08-19
  • 2016-04-04
相关资源
最近更新 更多