Log to Firefox Error Console from JavaScript
Solution 1
You cannot write to the console directly from untrusted JavaScript (e.g. scripts coming from a page). However, even if installing Firebug does not appeal to you, I'd recommend checking out Firebug Lite, which requires no installation into the browser (nor, in fact, does it even require Firefox). It's a script which you can include into any web page (even dynamically), which will give you some basic Firebug functionality (such as console.log()
).
Solution 2
If you define a global function that checks for the existence of window.console, you can use Firebug for tracing and still plays nice with other browsers and/or if you turn Firebug's console tracing off:
debug = function (log_txt) {
if (typeof window.console != 'undefined') {
console.log(log_txt);
}
}
debug("foo!");
Solution 3
Yes, you can =P
function log(param){
setTimeout(function(){
throw new Error("Debug: " + param)
},0)
}
//Simple Test:
alert(1)
log('This is my message to the error log -_-')
alert(2)
log('I can do this forever, does not break')
alert(3)
Update to a real function
This is a simple hack, just for fun.
Solution 4
window.console is undefined in Firefox 4 beta 6 even if Firebug 1.6X.0b1 is enabled and open, probably because of privilege issues that others discuss. However, Firefox 4 has a new Tools > Web Console, and if this is open you have a window.console object and untrusted JavaScript code on the page can use console.log(). The Web Console is in flux (see https://wiki.mozilla.org/Firefox/Projects/Console), you may need to change settings named devtools.* in about:config , YMMV.
Solution 5
I would just install Firebug and use console.log
. If you can't do that, though, you can always throw an error:
throw "foobar";
throw new Error("bazquux");
Of course, this will break you out of the code that you're currently executing, so you can't use it for detailed logging, but if you can work around that I think it's the only way to get something logged out of the box.
Torsten Marek
Updated on July 09, 2022Comments
-
Torsten Marek almost 2 years
Is it possible to add messages to the built-in error console of Firefox from JavaScript code running in web pages?
I know that I there's Firebug, which provides a
console
object and its own error console, but I was looking for a quick fix earlier on and couldn't find anything.I guess it might not be possible at all, to prevent malicious web pages from spamming the log?
-
Torsten Marek about 15 yearsI'm not against installing Firebug (which I have by now), I just wanted to know for sure. Throwing an error explictly wasn't an option, because I was tracing my code.
-
Kevin L. over 14 yearsIf you want a non-blocking error message (e.g., monitoring a variable in a loop), use setTimeout("throw new Error('Wheeeeeeee!')",0);
-
bobobobo over 14 yearsbazquux is EXACTLY the type of error I wanted to notify myself about
-
Fabiano Soriani about 13 years@nornagon Yeah, I get the point, it would be nicer with a function, but I made this way in order to be compatible with IE bad setTimeout()
-
Mike Miller about 13 years+1 for cleverness. However I think I will go the firebug route. :)
-
jamesmortensen almost 13 yearsThis throws a permissions error in Firefox 4: 'Permission denied to get property XPCComponents.classes'.
-
MarcH almost 13 yearsThanks for the feedback jmort253. Did you try to grant the capability in user_pref as demonstrated in a comment in the code?
-
jamesmortensen almost 13 yearsThat was a long time ago so I don't exactly recall... I think you can see it in Firebug. You're probably right about the user_pref needing to be set, but that would mean the log could only be useful to the developer. It's hard to get users to change things like that.
-
MarcH almost 13 yearsEven with Firefox 3 this function required to grant full privileges to the website, which definitely makes it useful to the developers only, there is no question about that. I guess they just hardened the security in Firefox 4, which makes sense considering how many users click "OK" without reading.
-
jamesmortensen almost 13 yearsOk, I see the value in it as a developer on the local machine. At first I was thinking that the logging system would run on regular users system and not just developers. I've seen some pretty crazy things happen in users browsers and it would be awesome if they could send me a log. I had a bug in something on a coworkers computer I couldn't replicate, until we determined it was because he had NoScript and Adblock Firefox extension installed, which prevented my JS from running!
-
Stefano over 12 years+1 for Components.utils.reportError - I keep forgetting how it's call and wasting time to find it again, over and over again!
-
Justin Tanner over 12 years+1 this is useful if you accidentally leave console.log's in your production code.
-
Clint Pachl over 12 yearsThis is the absolutely perfect solution for the question asked.
-
ceving over 12 yearsIt is not necessary to wrap the console.log function. By wrapping it you will loose the formating functionality. Just assign it to a debug function:
var debug = function () {} ; if (window.console != undefined) { debug = console.log; }
. Now you can also do this:debug ("%s", "a")
-
Ruggero Turra almost 12 years@ceving: do you know why with your solution in chrome I get:
TypeError: Illegal invocation
? -
ceving almost 12 years@wiso: Chrome might require a wrapper. See here: stackoverflow.com/questions/8159233/…
-
Bass over 8 yearsAnd in the SeaMonkey browser (which is also Gecko-based), you can enable logging to the JavaScript console via
browser.dom.window.console.enabled
hidden preference (since 2.11, see bug 739965). This doesn't affect FireBug, as allconsole.log(...)
results immediately appear in its console regardless of the pref.