Yuval Sarel
Yuval Sarel

Reputation: 15

how can i define iframe to have null content?

I have this code and I want after every click the content of the iframe will be null and only later

<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>

Thank you so much for the help (-;

p.s.- I try to make my editor

Upvotes: 0

Views: 57

Answers (1)

mplungjan
mplungjan

Reputation: 177684

You need to do

frame.contentWindow.document.close(); 

after each write, then the next write will clear it.

Also why not use frameDoc now you have it?

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(); 
}

Also make the button type="button some browsers will submit the page if not

Upvotes: 1

Related Questions