Skip to content

Instantly share code, notes, and snippets.

@AndersDJohnson
Last active June 5, 2019 13:35
Show Gist options
  • Save AndersDJohnson/5692405 to your computer and use it in GitHub Desktop.
Save AndersDJohnson/5692405 to your computer and use it in GitHub Desktop.
dom logger / jsfiddle
/**
* Originally based on http://www.kobashicomputing.com/send-your-console-output-to-the-result-pane-in-jsfiddle
*/
window.jsFiddleConsole = function() {
var consoleDiv;
consoleDiv = document.createElement('div');
document.body.appendChild(consoleDiv);
return {
log: function(msg) {
var para, text;
para = document.createElement('p');
text = document.createTextNode(msg);
para.appendChild(text);
consoleDiv.appendChild(para);
}
};
};
(function(context){
// don't call until domready
var getDefaultElement = (function () {
var defaultElement;
return function () {
if (typeof defaultElement == 'undefined') {
defaultElement = document.createElement('div');
document.body.appendChild(defaultElement);
}
return defaultElement;
};
})();
var Logger = function(name, element) {
this.name = name;
this.element = element || getDefaultElement();
};
Logger.prototype.log = function(msg) {
var para, text;
msg = this.name + ': ' + msg;
para = document.createElement('p');
text = document.createTextNode(msg);
para.appendChild(text);
this.element.appendChild(para);
};
context.Logger = Logger;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment