Created
March 2, 2019 09:39
-
-
Save mhofman/fe170f9492196e9eb09907c84ea28999 to your computer and use it in GitHub Desktop.
Simple HTML Console library
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
(function (global) { | |
var output = null; | |
var buffer = []; | |
var target; | |
function log(msg) { | |
if (!output) { | |
buffer.push(msg); | |
return; | |
} | |
var pre = document.createElement('pre'); | |
pre.textContent = msg; | |
output.appendChild(pre); | |
} | |
if (global.htmlConsole) { | |
target = global.htmlConsole.target; | |
} | |
if (!target) { | |
target = "#console"; | |
} else if (typeof target.appendChild == 'function') { | |
output = target; | |
} | |
if (!output) document.addEventListener("DOMContentLoaded", function(){ | |
output = document.querySelector(target); | |
if (!output) throw new Error('No element found for console using query "' + target + '".') | |
buffer.map(log); | |
buffer.length = 0; | |
}); | |
global.htmlConsole = global.htmlConsole || {}; | |
global.htmlConsole.log = log; | |
}(this)); |
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 lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Simple HTML Console example</title> | |
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"> | |
<script src="./html-console.js"></script> | |
<script> | |
htmlConsole.log('Hello from page'); | |
setTimeout(() => htmlConsole.log('Delayed message'), 1000); | |
</script> | |
</head> | |
<body> | |
<div id="console"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment