【发布时间】:2017-03-10 15:31:01
【问题描述】:
我有一个页面,我希望它将描述它的 javascript 转储到 <code> 块中。问题是如果我使用$('pre code').html(data);,浏览器会解释脚本字符串文字中的标签并且格式会中断。相反,如果我使用$('pre code').text(data);,那么我的脚本将被执行,导致页面被渲染两次并复制了一些元素。
我知道这是一个奇怪的用例,但是这种问题有什么规范的解决方案吗?我目前的解决方案只是渲染盒子并同时执行脚本。
$.get("index.js", function(data, status)
{
//Populate the code box and rn the page javascript at the same time
//It seems to be impossible to set the text without making the javascript execute
$('pre code').text(data);
$('pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
});
【问题讨论】:
标签: javascript jquery escaping xss