weixin_33695082 2019-07-10 20:44 采纳率: 0%
浏览 166

未捕获的SyntaxError:

The code should make a pop up window, asking for confirmation "press ok to confirm the action for user" but it doesn't. i'm all out of ideas.

function friendToggle(type,user,elem){
var conf = confirm("Press OK to confirm the '"+type+"' action for user 
<?php echo $u; ?>.");
if(conf != true){
    return false;
}
$(elem).innerHTML = 'please wait ...';
var ajax = ajaxObj("POST", "php_parsers/friend_system.php");
ajax.onreadystatechange = function() {
    if(ajaxReturn(ajax) == true) {
        if(ajax.responseText == "friend_request_sent"){
            $(elem).innerHTML = 'OK Friend Request Sent';
        } else if(ajax.responseText == "unfriend_ok"){
            $(elem).innerHTML = '<button onclick="friendToggle(\'friend\',\'<?php echo $u; ?>\',\'friendBtn\')">Request As Friend</button>';
        } else {
            alert(ajax.responseText);
            $(elem).innerHTML = 'Try again later';
        }
    }
}
ajax.send("type="+type+"&user="+user);
}
</script>

PHP code:

<?php 
$friend_button = '<button disabled>Request As Friend</button>';
$block_button = '<button disabled>Block User</button>';
// LOGIC FOR FRIEND BUTTON
if($isFriend == true){
    $friend_button = '<button onclick="friendToggle(\'unfriend\',\''.$u.'\',\'friendBtn\')">Unfriend</button>';
}
 else if($user_ok == true && $u != $log_username && 
$ownerBlockViewer == false){
    $friend_button = '<button onclick="friendToggle(\'friend\',\''.$u.'\',\'friendBtn\')">Request As Friend</button>';
}
// LOGIC FOR BLOCK BUTTON
if($viewerBlockOwner == true){
    $block_button = '<button onclick="blockToggle(\'unblock\',\''.$u.'\',\'blockBtn\')">Unblock User</button>';
} else if($user_ok == true && $u != $log_username){
    $block_button = '<button onclick="blockToggle(\'block\',\''.$u.'\',\'blockBtn\')">Block User</button>';
}
?> 

Opening up the console i see "Uncaught SyntaxError: Invalid or unexpected token"

  • 写回答

1条回答 默认 最新

  • weixin_33674976 2019-07-10 21:05
    关注

    The console issue is with lines 2 & 3 of your JavaScript code;

    var conf = confirm("Press OK to confirm the '"+type+"' action for user
    <?php echo $u; ?>.");
    

    You have a multi-line String which the console is not interpreting as one String value. In order to resolve, ECMAScript 6 (ES6) introduced template literals which can be utilized like below to handle multi-line Strings;

    var conf = confirm(`Press OK to confirm the '"+type+"' action for user
    <?php echo $u; ?>.`);
    

    (i.e. use back-ticks rather than double quotes to start & end the multi-line String)

    Or, if ES6 isn't supported you can use good old String concatenation like below;

    var conf = confirm("Press OK to confirm the '"+type+"' action for user" +
    "<?php echo $u; ?>.");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM