SyntaxError: identifier starts immediately after numeric literal in Firebug

瘦欲@ 提交于 2019-11-27 07:59:13

问题


I'm getting that error when I call this javascript function:

function kickUser(id_userChat){
$.post("chatFuncs.php", { action: "kick", id_user: id_userChat });  
}

this "kickUser" function is generated for every user connected to my chat box, like this

$listUsers .= '<img src="imgUsers/'.$DBClass->nomImg($rowUsers['id_user'],$posImg).'" height="'.$heightImg.'" width="'.$widhImg.'"/>
<span class="styleMsg">'.$rowUser['nameUser'].'</span>&nbsp;
<a href="#" class="BtnKick" onClick="kickUser('.$rowUsers['id_user'].')">Kick</a></br>';

and the action "kick" is just an update to my database where I remove the user from my chatUsers table

If I change $rowUsers['id_user'] for $rowUsers['userName'] the error changes to: ReferenceError: 'userName' is not defined (i changed the real name of the user for 'userName' just for this example).


回答1:


Identifiers in JavaScript can't begin with a number; they must begin with a letter, $ or _.


I'm guessing it's coming from this:

onclick="kick_user('.$rowUsers['id_user'].')">Kick</a>

If you mean to pass a string, then you need to quote the value being passed.

onclick="kick_user(\"'.$rowUsers['id_user'].'\")">Kick</a>

I don't know PHP, so maybe you need different escaping, but this should give you the idea.




回答2:


The resulting JavaScript code will be

kickUser(userName)

…and obviously there is no js variable userName. You want to pass a string instead:

kickUser('userName');

So add the quotes/apostrophes to the output, and don't forget to escape the $rowUsers['userName'] properly. It's quite the same for $rowUsers['id_user'], which seems to have output even an invalid identifier.



来源:https://stackoverflow.com/questions/14966133/syntaxerror-identifier-starts-immediately-after-numeric-literal-in-firebug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!