Created
May 24, 2012 19:42
-
-
Save toolness/2783783 to your computer and use it in GitHub Desktop.
live preview test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<title>live preview test thing</title> | |
<style> | |
textarea { | |
display: block; | |
width: 100%; | |
min-height: 200px; | |
} | |
iframe { | |
border: 1px solid black; | |
width: 100%; | |
min-height: 200px; | |
} | |
</style> | |
<textarea id="code" | |
onkeyup="updatePreview()"><h1>hi</h1></textarea> | |
<script> | |
var iframe, | |
pageX = 0, | |
pageY = 0; | |
function updatePreview() { | |
var html = document.getElementById("code").value; | |
if (iframe) { | |
pageX = iframe.contentWindow.pageXOffset; | |
pageY = iframe.contentWindow.pageYOffset; | |
iframe.parentNode.removeChild(iframe); | |
} | |
iframe = document.createElement('iframe'); | |
document.body.appendChild(iframe); | |
var doc = iframe.contentDocument; | |
var wind = iframe.contentWindow; | |
doc.open(); | |
doc.write(html); | |
doc.close(); | |
wind.scroll(pageX, pageY); | |
} | |
onload = updatePreview; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment