Alright, this does what you want:
Code:
<textarea id="field" onfocus="getFocus();" onblur="loseFocus();">This is some text.</textarea>
<iframe name="target"></iframe>
<script>
var beforeText = '';
var textarea = document.getElementById('field');
var iframe = window.target.document;
function displayResult() {
if (textarea.value && textarea.value != beforeText) {
iframe.open();
iframe.write(textarea.value);
iframe.close();
beforeText = textarea.value;
alert(beforeText);
}
window.setTimeout(displayResult, 10);
}
function getFocus() {
if (textarea.value == textarea.defaultValue) {
textarea.value = '';
}
}
function loseFocus() {
if (textarea.value == '') {
textarea.value = textarea.defaultValue;
}
}
displayResult();
</script>
this jsfiddle demonstrates it by alerting when the function is called. Rather than changing every 10ms, it runs only if it has been updated.
Bookmarks