如何将XMLHttpRequest检索的数据放入iframe?

时间:2012-01-18 22:47:53

标签: javascript iframe xmlhttprequest

我正在尝试向客户端演示XSS漏洞的后果,并且这样做我需要检索一个页面,通过简单的搜索和替换来更改其中的一些html,然后最终在一个页面中显示数据iframe中。我已经找到了前两部分,但我正在努力让iframe工作。

这是我到目前为止所拥有的:

<html>
<head>
<script type="text/javascript">
function getSource()
{
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    var content=xmlhttp.responseText;
    document.write(content.replace(/MyString/gi, "StringtoReplace"));
    }
  }
xmlhttp.open("GET","indextest.html",true);
xmlhttp.send();
</script>
</head>
<body>
<body onload="getSource()">
</body>
</html>

那我怎么把它放到iframe中呢?我一直在尝试将document.write行放入变量并将其作为iframe src调用,但到目前为止还没有运气。有什么建议?非常感谢

1 个答案:

答案 0 :(得分:1)

不是使用document.write()编写,而是使用:

var iframe = document.getElementById("YOUR IFRAME ID HERE");
iframe.contentDocument.write("HTML TAGS AND OTHER STUFF HERE");