Skip to content

Instantly share code, notes, and snippets.

@m0veax
Created July 22, 2020 09:14
Show Gist options
  • Save m0veax/4ccafc9e97b385b89e886f1653fba519 to your computer and use it in GitHub Desktop.
Save m0veax/4ccafc9e97b385b89e886f1653fba519 to your computer and use it in GitHub Desktop.
Redirect console output to a given div
// SOURCE https://stackoverflow.com/questions/16616722/sending-all-javascript-console-output-into-a-dom-element
var baseLogFunction = console.log;
console.log = function(){
baseLogFunction.apply(console, arguments);
var args = Array.prototype.slice.call(arguments);
for(var i=0;i<args.length;i++){
var node = createLogNode(args[i]);
document.querySelector("#mylog").appendChild(node);
}
}
function createLogNode(message){
var node = document.createElement("div");
var textNode = document.createTextNode(message);
node.appendChild(textNode);
return node;
}
window.onerror = function(message, url, linenumber) {
console.log("JavaScript error: " + message + " on line " +
linenumber + " for " + url);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment