2013-01-08 34 views
1

我正在爲我的網站創建聊天。它工作正常,但功能不在HTML按鈕內部工作。我有3個文件用於它,一行不工作。Javascript錯誤:未捕獲SyntaxError:意外令牌}

之前,我有一個問題,我設置了Javascript函數,創建按鈕,它會說該函數不存在。我解決了這個問題,把按鈕下面的功能。

每個按鈕顯示一個在線會員,他在你的朋友名單上。

我弄糊塗了我的錯誤,因爲我不知道這是如何造成的。

Error: 'Uncaught SyntaxError: Unexpected token } users.php:3'

(users.php是我在頁面上,如果我改變它,改變頁面名稱)

隨着鉻,我可以點擊錯誤,帶來了這一點:「 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'; 
    } 
?> 
+0

一切作品,但在線用戶的按鈕功能onClick ='chatWith();' –

+0

您可能希望查看您的PHP代碼,以確保您在select語句中沒有SQL注入風險,因爲您沒有清理或檢查來自客戶端的輸入。用戶名「O'Rielly」(或更糟!)會發生什麼? –

回答

1

嘗試改變PHP爲按鈕或類添加一個id屬性,如果將有多個。此外,刪除onclick屬性

<button class="btnChat" ... 

剛剛意識到,您還需要添加參數。我會將這些添加到按鈕上的數據屬性。

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

您需要確保$ url和$用戶N是正確轉義

然後在腳本2文件,你可以連接該按鈕的單擊事件:

$(function(){ 

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

}); 
+0

嗯,我不能那樣做。有信息表明PHP獲取javascript函數。然後,我可以使用ajax來獲取該函數內部的信息。我會嘗試。 –

+0

我的點擊功能不同。我可以做$('。btnChat')。click(chatWith(something,something)); ? –

+0

剛剛意識到,編輯答案 –

相關問題