Created
July 22, 2020 09:14
-
-
Save m0veax/4ccafc9e97b385b89e886f1653fba519 to your computer and use it in GitHub Desktop.
Redirect console output to a given div
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
// 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