javascript - 使用 javascript 检查用户输入是否包含 <script> 标记。意外的标记 {

标签 javascript html forms xss

<!DOCTYPE html>
<html>
<head>
    <script>
        function XSSPrevent(){
            var usercomments = document.getElementById('usertext').value;
            if(usercomments.contains("<script>"){
                alert("Failed");
            } else
            alert("Thank you for your comments" + usercomments);
        }
    </script>
</head>
<body>
    <form>
        <input type="text" id="usertext" maxlength=50/>
        <input type="submit" value="Enter" onclick="XSSPrevent()"/>
    </form>
</body>

这是我第一次使用 HTML 表单和 javascript。控制台

在 if 循环后显示意外标记 {。怎么了?
我知道在这种情况下使用警报方法不是一个好主意,但我

只是想快速检查一下我的功能。

最佳答案

你缺少一个括号:

function XSSPrevent(){
    var usercomments = document.getElementById('usertext').value;
    if(usercomments.contains("<script>")){ // <-- was missing ) here
        alert("Failed");
    } else
    alert("Thank you for your comments" + usercomments);
}

关于javascript - 使用 javascript 检查用户输入是否包含 &lt;script&gt; 标记。意外的标记 {,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45523153/

相关文章:

javascript - 使用带有数据数组的 Jquery 创建密度热图图表

javascript - JSON 中的 HTML

javascript - 如何使 <a href> 发出 GET 请求而不重定向?

javascript - 如何在 wordpress 上移动 iframe?

html - SVG 悬停时旋转虚线圆圈边框

php - 使用 jQuery/Ajax 提交表单只能每隔一段时间有效

javascript - CSS 旋转窗外

html - 使用伪元素(及其高度)的视差

javascript - Meteor.js 与 Iron Router 按钮和模板中的表单不起作用

jquery - 使用 jQuery Validate 一次检查表单的各个部分