SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how not to let console.log() to cause error on IE or other browsers

    sometimes we solely use console.log() to debug on Firefox. (i think it is only defined when Firebug is installed?) And the code will break IE or any Firefox that doesn't have Firebug installed, since console and console.log will be undefined.

    so i wonder how about disabling it just by

    if (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} };

    at the very top of javascript code. It is true we could use

    if (debugging) console.log(oDiv)

    but then we need to constant turn debugging on and off on different browsers, and it is more troublesome to use "if (debugging)" every time.

    This following line will work too:

    if (typeof console == "undefined") var console = { log: function() {} };

    except there might be some browser in which console is defined, but console.log is undefined... and using console.log() in those browser will cause an error. So the first solution in this post that test for both console and console.log is a better check.
    Last edited by winterheat; Oct 5, 2008 at 14:24.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    That sounds like an interesting idea. I would have thought though that cross-browser testing would have caught anything untoward that remained.

    I'm also not keen on the idea of having some console.log commands remain in production code that will get seen by visitors who also use firebug.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    That sounds like an interesting idea. I would have thought though that cross-browser testing would have caught anything untoward that remained.

    I'm also not keen on the idea of having some console.log commands remain in production code that will get seen by visitors who also use firebug.
    yeah, when it is production, we can remove all the console.log() calls...

    or if we don't care about them remaining in the code, we can use

    var debugging = false; // or true
    if (!debugging || typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} };


    hm... but I think Firefox with Firebug doesn't let us redefine console... so maybe there is another way... (just found out that console.log can be reassigned on Firefox with Firebug) something like

    var debugging = false; // or true
    if (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} };
    else if (!debugging && typeof console != "undefined") console.log = function() {};


    you meant to use Browser sniffing to define console.log()? I tried it too and since my Firefox on Mac doesn't have Firebug, it gave error for using console.log() when it is not defined.

  4. #4
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually, i think this is a better solution:

    var debugging = false; // or true
    if (typeof console == "undefined") var console = { log: function() {} };
    else if (!debugging || typeof console.log == "undefined") console.log = function() {};


    the logic goes:

    if console is undefined, that means it is on IE or Firefox without Firebug, so just make console.log to be an empty function.
    otherwise, console is defined. but could there be some browser that actually doesn't have console.log defined? In that case, set console.log to an empty function. Also, when console is defined but we are not in debugging mode, then also set console.log to an empty function.
    Last edited by winterheat; Oct 5, 2008 at 15:05.

  5. #5
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    window.log = function ( string ) {
    if ( typeof console == 'object' ) { console.log ( string );
    }

  6. #6
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SoulScratch View Post
    window.log = function ( string ) {
    if ( typeof console == 'object' ) { console.log ( string );
    }
    and then just use window.log() in our code for print debug string huh?

    console.log accepts multiple parameters... but window.log can probably be made to do that too...

    also it checks console every time. so can we do

    if (typeof console == "object") window.log = console.log;
    else window.log = function() {}
    ;

    and since window is the current object, we can just use

    if (typeof console == "object") log = console.log;
    else log = function() {};


    and then use

    log(123, oDiv, s); to debug our code.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •