Web Hosting Talk







View Full Version : Javascript iframe problem


BurakUeda
10-15-2007, 05:10 AM
I can read the contents of an iframe:

var iframe = document.getElementById("message");
var iframeContents = iframe.contentDocument.body.innerHTML;

var txtarea = document.getElementById('message2');
txtarea.value = iframeContents;
This will read the contents of an iframe, and write it to a textarea.
iframe is editable, and content is entered by the user. Still, this works.

But, below code is not working. Trying to put some content to an iframe, with the same innerHTML method above. It returns no errors or warnings. Even last alert() displays "<div>lorem ipsum</div>", but iframe is empty, it displays nothing..

var textContent = "lorem ipsum";
var iframe2 = document.getElementById('message');
iframe2.contentDocument.body.innerHTML = '<div>'+textContent+'</div>';

alert(iframe.contentDocument.body.innerHTML);

Any help greatly appreciated.

BurakUeda
10-15-2007, 05:32 AM
Sorry guys, I found the problem.
It was the speed of the executing the script, again....
It proceeds and executes next line, without receiving the results of the current line.

JavaScript seems to have this problem if you create elements on the fly with createElement function. I put the above script into a function, set a timeout: setTimeout(delayInsert,250) and problem solved.