我有一个django Web应用程序,我想允许用户在textareas中键入HTML,CSS和JS,然后在iFrame中显示它们。如何安全地执行此操作?
答案 0 :(得分:0)
好问题。它可能有助于您防止XSS。无论如何,这是一个小例子。在这里,我用Div
代替了iFrame
<h1>Unsafe Mode:</h1>
<p>
Enter HTML Code below
</p>
<textarea cols=50 rows=5 id='unsafecode'>
</textarea>
<br>
<button onclick='show_unsafe()'>Show Code</button>
<br>
<div id='unsafecont'>
</div>
<br>
<h1>Safe Mode:</h1>
<p>
Enter HTML Code below
</p>
<textarea cols=50 rows=5 id='safecode'>
</textarea>
<br>
<button onclick='show_safe()'>Show Code</button>
<br>
<div id='safecont'>
</div>
<script>
function show_unsafe(){
document.getElementById('unsafecont').innerHTML=document.getElementById('unsafecode').value;
}
function show_safe(){
document.getElementById('safecont').innerText=document.getElementById('safecode').value;
}
</script>