Created
April 25, 2012 08:53
-
-
Save marijnh/2488301 to your computer and use it in GitHub Desktop.
html + css preview
This file contains hidden or 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> | |
<html> | |
<head> | |
<title>HTML&CSS SANDBOX</title> | |
<meta charset=utf-8> | |
<script src=lib/codemirror.js></script> | |
<script src=mode/xml/xml.js></script> | |
<script src=mode/javascript/javascript.js></script> | |
<script src=mode/css/css.js></script> | |
<script src=mode/htmlmixed/htmlmixed.js></script> | |
<link rel=stylesheet href=lib/codemirror.css> | |
<link rel=stylesheet href=doc/docs.css> | |
<link rel=stylesheet href=style.css> | |
</head> | |
<body> | |
<div id="container"> | |
<h1>HTML&CSS SANDBOX</h1> | |
<textarea id=htmlcode name=htmlcode></textarea> | |
<textarea id=csscode name=csscode></textarea> | |
<iframe id=preview></iframe> | |
</div> | |
<script> | |
function addStyleElement() { | |
var previewStyle = document.getElementById('preview'); | |
var addstyle = previewStyle.contentDocument || previewStyle.contentWindow.document; | |
addstyle.createElement('style'); | |
addstyle.head.appendChild(style); | |
} | |
var delay; | |
var htmleditor = CodeMirror.fromTextArea(document.getElementById('htmlcode'), { | |
mode: 'text/html', | |
lineNumbers: true, | |
lineWrapping: true, | |
tabMode: 'indent', | |
onChange: function() { | |
clearTimeout(delay); | |
delay = setTimeout(updatePreview, 300); | |
} | |
}); | |
var delay; | |
var csseditor = CodeMirror.fromTextArea(document.getElementById('csscode'), { | |
mode: 'text/css', | |
lineNumbers: true, | |
lineWrapping: true, | |
tabMode: 'indent', | |
onChange: function() { | |
clearTimeout(delay); | |
delay = setTimeout(updatePreview, 300); | |
} | |
}); | |
function updatePreview() { | |
var previewFrame = document.getElementById('preview'); | |
var addhtml = previewFrame.contentDocument || previewFrame.contentWindow.document; | |
var html = htmleditor.getValue(); | |
var css = csseditor.getValue(); | |
if (/<\/head\b/.test(html)) html = html.replace(/<\/head\b/, "<style>" + css + "</style></head"); | |
else html = "<style>" + css + "</style>" + html; | |
addhtml.open(); | |
addhtml.write(html); | |
addhtml.close(); | |
} | |
setTimeout(updatePreview, 300); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment