Reputation: 14365
I have a browser that doesn't have any console. I need to redirect the output to my home made console.
Is there a way to achieve that ?
Upvotes: 1
Views: 61
Reputation: 1074335
If it's just errors you want to catch, some of them will go to the window.onerror
handler.
window.onerror = function(errorMsg, url, lineNumber) {
// ...do something with the information...
// Return false if you want to prevent the browser's default behavior
return false;
};
Upvotes: 3
Reputation: 193261
You can create very simple custom "console" with few lines of HTML and javascript. For example:
var xConsole = {
log: function(msg) {
var output = '<div>' + msg + '</div>';
document.getElementById('console').insertAdjacentHTML('beforeend', output);
}
};
xConsole.log('test');
xConsole.log(new Date().getTime());
<pre id="console"></pre>
Then you will need to put this HTML line somewhere in the document (or append it dynamically). You could also use some CSS to style it or position it as you like.
UPD. Improved per comment by T.J. Crowder. Here is possible very basic solution that would also support error catching:
// uncomment below line and remove "if (true) {" when used in real world
//if (!window.console) {
if (true) {
window.console = function() {
var _ = {};
_.log = function(msg) {
var output = '<div>' + msg + '</div>';
document.getElementById('console').insertAdjacentHTML('beforeend', output);
};
window.onerror = function(errorMsg) {
_.log(errorMsg);
};
return _;
}();
}
// Should log simple message
console.log('test message');
// Should log reference error
test + 1;
<pre id="console"></pre>
Upvotes: 1