简体   繁体   English

如何在JavaScript回声中转义HTML

[英]How escape html in echo of javascript

I can't escape my code JS in my echo : 我无法在echo中逃脱我的代码JS:

echo "<script type='text/javascript'>
    // Initialiser l'objet.
    var tab_nb_match_par_user = ".json_encode($tab_nb_match_par_user).";
    var taille = 70;    
    console.log(tab_nb_match_par_user);
    for (var k in tab_nb_match_par_user){
        $('#img').append('<img src="stats_matching/' + k + '.gif" alt="' + k + '" title="' + k + '" width="' + taille + '" />');
        taille = taille-6;
    }
</scrip>";

And I have this follow error: Uncaught SyntaxError: missing ) after argument list 而且我有以下跟随错误: Uncaught SyntaxError: missing ) after argument list

Use HEREDOC instead of echo. 使用HEREDOC代替echo。

echo <<<ENDOFSCRIPT
<script type='text/javascript'>

                    // Initialiser l'objet.
                    var tab_nb_match_par_user = ".json_encode($tab_nb_match_par_user).";
                    var taille = 70;    
                    console.log(tab_nb_match_par_user);
                    for (var k in tab_nb_match_par_user){
                        $('#img').append('<img src="stats_matching/' + k + '.gif" alt="' + k + '" title="' + k + '" width="' + taille + '" />');
                        taille = taille-6;
                    }
            </script>

ENDOFSCRIPT;

more info at https://wiki.php.net/rfc/heredoc-with-double-quotes 有关更多信息, 请参见https://wiki.php.net/rfc/heredoc-with-double-quotes

Assuming that you only want to actually have php fill in the json part, why not just echo in the bit you need, rather than the whole structure. 假设您只想将php实际填充到json部分中,为什么不回显所需的位,而不是整个结构。 This is much better practice in templating. 这是模板更好的做法。

<script type='text/javascript'>
    // Initialiser l'objet.
    var tab_nb_match_par_user = "<?php echo json_encode($tab_nb_match_par_user) ?>";
    var taille = 70;    
    console.log(tab_nb_match_par_user);
    for (var k in tab_nb_match_par_user){
        $('#img').append('<img src="stats_matching/' + k + '.gif" alt="' + k + '" title="' + k + '" width="' + taille + '" />');
        taille = taille-6;
    }
 </script>

you were also missing a 't' in your end script tag. 您在结束脚本标记中也缺少“ t”。

echo <<<END
    <script type="text/javascript">
        // Initialiser l'objet.
        var tab_nb_match_par_user = ".json_encode($tab_nb_match_par_user).";
        var taille = 70;    
        console.log(tab_nb_match_par_user);
        for (var k in tab_nb_match_par_user){
            $('#img').append('<img src="stats_matching/' + k + '.gif" alt="' + k + '" title="' + k + '" width="' + taille + '" />');
            taille = taille-6;
        }
    </script>
END;

You need to escape the code using a back slash: 您需要使用反斜杠对代码进行转义:

echo "<script type='text/javascript'>

                        // Initialiser l'objet.
                        var tab_nb_match_par_user = ".json_encode($tab_nb_match_par_user).";
                        var taille = 70;    
                        console.log(tab_nb_match_par_user);
                        for (var k in tab_nb_match_par_user){
                            $('#img').append('<img src=\"stats_matching/' + k + '.gif\" alt=\"' + k + '\" title=\"' + k + '\" width=\"' + taille + '\" />');
                            taille = taille-6;
                        }
                </scrip>";

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

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