<!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 检查用户输入是否包含 <script> 标记。意外的标记 {,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45523153/