1

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 ?

2
  • Where does the home made console live? Commented Feb 10, 2015 at 14:00
  • @Halcyon it's a fixed div into the webview Commented Feb 10, 2015 at 14:42

2 Answers 2

3

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;
};
Sign up to request clarification or add additional context in comments.

4 Comments

Why "some of" ? I've checked you're link, have you tried that in production?
@BigDong: That comment is based purely on the MDN page I linked; MDN is collaboratively-edited and therefore sometimes wrong, but usually right. I don't know in this case whether it is or not. I wondered if it might relate to code that couldn't even be parsed (e.g., in a separate script block), but in my tests of Chrome, Firefox, and IE11, all three called onerror even in that case.
I have tried on android browser, it didn't worked at all
@BigDong: I can verify that on Android 2.3 (it doesn't work). My Android 4 devices are packed away at the moment, can't check them. Chrome for Android apparently works, but not the built-in browser. This page seems to have some info.
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.