Javascript - Empty an iframe

Solved
le sparte -  
 Chaostronix -
Hello,

My question is very simple: I have an iframe filled in Javascript with the command "document.write('my text')" and I’m looking for a way to clear it of its content so that with every user click, the old content of the iframe is overwritten by the new one.

Thanks in advance for your help ;-)

Configuration: Mac OS X Firefox 2.0.0.3

4 answers

kij_82 Posted messages 4102 Registration date   Status Contributor Last intervention   857
 
Look at the innerHTML side. Usage: You retrieve the element in the following way (let’s suppose your iframe is named 'toto' -> id='toto') and you replace its content with "".
 var myIframe = document.getElementById('toto'); myIframe.innerHTML = "";
But having never used iframes, I’m not sure whether it’s a component like DIVs, or a component managed like a frame. If it’s the first way, then the method above should work. If an iframe is managed in the same way as a Frame, then you would need to do as follows:
 var myIframe = parent.frame['toto']; myIframe.document.innerHTML = "";
I’m not sure this works but it’s a lead (I can’t test, sorry). Good luck for the rest. -- ~ Don’t forget the "Resolved" tag when your problem is... resolved :) ~
0
le sparte
 
Hello and thanks for the quick response! Thanks to your help, I’ve made progress solving my problem, but another one has emerged. Here is the script below: ecrire dans un iframe
If you click remplir, it works. If you click effacer, it works. But if you click remplir again... it doesn’t work! How can I avoid this? Thanks in advance for your help! ;-)
0
2i4u > le sparte
 
Hi, I have exactly the same problem as you and I wanted to know if you found a solution to your issue? Thanks
0
kij_82 Posted messages 4102 Registration date   Status Contributor Last intervention   857
 
Hello, Why use document.write in the remplir function, and document.body.innerHTML in vider? Why not use the same instruction in both functions? If you replace the code in your 'remplir' function with this:
 document.getElementById("htmle").contentWindow.document.body.innerHTML = "blablabla"; 
Will it work? -- ~ Do not forget the "Resolved" tag when your problem is... resolved :) ~
0
Aguila
 
Thank you for your help, this helped me solve my problem.
I confirm the following syntax: document.getElementById('id_de_la_frame).contentWindow.document.body.innerHTML in order to read the content of my iframe.

Have a great day everyone.
0
Chaostronix
 
Hi,

After doing:
document.getElementById("htmle").contentWindow.document.write('blablabla');

You should do:
document.getElementById("htmle").contentWindow.document.close();

Otherwise the document remains open and the browser spins its wheels.

This allows your clear function to work correctly as many times as you want.
0