简体   繁体   中英

Javascript + Firebug console.log(), how to not get exceptions?

I'm writing a javascript / HTML5 canvas library to provide basic GUI elements for web audio applications.

There's a little demo script that creates widgets with the library and assemble them in a GUI. You can find it @ http://bitterspring.net/webshifter/

The problem is, it seems to work correctly on Chrome and on Firefox 3.6 - 4.0 but, in the last cases, only with firebug . Without firebug, the script seems to visualize nothing on screen, while with firebug it does.

The only firebug-related pieces of code are some console.log statement I use to track the behaviour of the library. But these statements should have no effect on a non-firebug enabled browser, as I learnt from Firebug forums. What can prevent the example script to work, in these cases?

The library + example code is freshly committed on http://github.com/janesconference/KievII , by the way.

EDIT: Seems that, when console is not defined, console.log() throws an exception. Is there a way to keep the logging lines of code and not getting the exception? (yeah, one could check if console != undefined, but is there a better way?)

EDIT: This does the trick, it seems ( Font )

if (typeof console=="undefined"){console={log:function(A){var B=false;if(B){alert(A)}}}}

Right, the console object is not available in all browsers by default.

This code:

if (typeof console=="undefined"){console={log:function(A){var B=false;if(B){alert(A)}}}}

- currently disables console support in Firefox 4's Web Console, since it tries to inject the console object when opened and won't do that if the page already defined a console object.

An interesting wrapper for console that deals with this problem is: http://benalman.com/projects/javascript-debug-console-log/ , although I haven't tried it myself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM