Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用javaScript打印HTML中的特定字符串_Javascript_Html_Error Handling_Xss - Fatal编程技术网

无法使用javaScript打印HTML中的特定字符串

无法使用javaScript打印HTML中的特定字符串,javascript,html,error-handling,xss,Javascript,Html,Error Handling,Xss,我在使用javaScript将这个字符串呈现为html时遇到了困难 ”尝试使用Node.textContent而不是Node.innerHTML: "<img onerror='window.document.body.textContent = \"<h1>XSS</h1>\";' src=''> " “” 如果希望整个html代码显示为文本,请使用innerText或textContent,因为它们不会试图呈现html代码 //using templa

我在使用javaScript将这个字符串呈现为html时遇到了困难
尝试使用
Node.textContent
而不是Node.innerHTML:

"<img onerror='window.document.body.textContent = \"<h1>XSS</h1>\";' src=''> "
“”

如果希望整个html代码显示为文本,请使用
innerText
textContent
,因为它们不会试图呈现html代码

//using template literals ` (backtick) to help not need escaping quotes
document.body.innerText = `<img onerror="window.document.body.innerHTML ='<h1>XSS</h1>';" src=''>`

如果要从呈现中跳过html代码,请使用
element.innerText

document.getElementById(“测试”).innerText=“XSS”

test
上述代码是跨站点脚本(XSS),这是一种常见于web应用程序中的计算机安全漏洞。XSS使攻击者能够将客户端脚本注入到其他用户查看的网页中。 您必须使用下面的函数清理字符串

String.prototype.escape=function(){
var tagsToReplace={
“&”:“&;”,
'': ''
};
返回此。替换(/[&]/g,函数(标记){
return tagsToReplace[tag]| | tag;
});

};当图像失败时,为什么要替换整个页面?欢迎使用StackOverflow!很难准确地找出到底出了什么问题,因为我们无法看到您是如何将其呈现为HTML的。您可以添加您在JavaScript中如何使用该字符串吗?
<img onerror="window.document.body.innerText ='<h1>XSS</h1>';" src=''>