如何定义iframe以获得null内容?

时间:2016-07-04 12:52:54

标签: javascript iframe

我有这段代码,我希望每次点击后iframe的内容都为空,只有稍后

<div id="code" contenteditable></div>
<iframe id="output"></iframe>
<button onclick="update()">run</button>

<script>
function update() {
var frame = document.getElementById("output");
var frameDoc = frame.contentDocument || frame.contentWindow.document;
var w = document.getElementById("code").textContent;

//************************************
//here is what i want to change
frame.contentWindow === null;
frame.contentWindow.document.write(w);
//************************************
}
</script>
<style>
#code{
width:47%;
height:81.8%;
border:1px solid;
}
#output{
width:47%;
height:80%;
border:1px solid;
position:absolute;
right:0px;
top:0px;
}

</style>

非常感谢你的帮助( - ;

p.s.-我尝试制作我的编辑

1 个答案:

答案 0 :(得分:1)

你需要做

frame.contentWindow.document.close(); 
每次写入后

,然后下一次写入将清除它。

为什么现在没有使用frameDoc呢?

function update() {
  var w = document.getElementById("code").textContent;
  var frame = document.getElementById("output");
  var frameDoc = frame.contentDocument || frame.contentWindow.document;
  frameDoc.write(w); 
  frameDoc.close(); 
}

如果没有按钮type="button某些浏览器会提交页面