Until recently I've used console.log extensively in Electron, both from code and monitoring results in the Developer Tools console, and also by typing console.log("something or other") directly in the Developer Tools console.
All of a sudden it's no longer working. Nothing happens when running console.log() from a JS script, and if I type console.log("test") in the Developer Tools console the response is "undefined". console.log by itself (no parentheses) shows ƒ (){} as opposed to function log() {[native code]}, which perhaps explains the lack of functionality?
Trying to figure out how that might have happened and how to resolve it -- did I accidentally click on some preference or setting somewhere? Quite the PITA to lose that.
where console.log() stopped working
console.context().log() is the alternative command that so far has been working.
Related
probably a silly question, but somehow i ended up on the debug function available in Google Chrome Console, which if i try to print it on the console i get:
ƒ debug(function, condition) { [Command Line API] }
however i'm totally unable to console.log something from it in any way, and I can't find any documentation online...
I've tested it on Firefox and i get Uncaught ReferenceError: debug is not defined so it's not cross platform, but maybe for development purpose it might be helpful
Maybe is related to debugger;?...
This is documented in the Console Utilities API Reference:
debug(function)
When the specified function is called, the debugger is
invoked and breaks inside the function on the Sources panel allowing
to step through the code and debug it.
debug(getData);
Use undebug(fn) to stop breaking on the function, or use the UI to
disable all breakpoints.
For more information on breakpoints, see Pause Your Code With
Breakpoints.
I would like to use console.log(message) to write out some information to the browser console. However, I came across this url which seems to recommend against it:
https://developer.mozilla.org/en-US/docs/Web/API/Console/log
Are you currently choosing to use console.log(message) as part of your js code? If not then have you identified an alternative?
I agree with Mike C above-- console is generally available in most browsers, but you should probably remove console logs before a site gets pushed to production.
Additionally, some older browser might not have the console, and if you did accidentally leave in a console log, it would fire an error when it attempted to interact with with something that wasn't defined. As an extra failsafe, you can declare console and console.log in the global namespace if they are not detected, just in case:
if (!console) {
console = {
log: function () { //noop }
};
}
should I use a js function other than console.log(message)?
simple answer is yes, But also the console.log(message) is usaually used for testing purposes and for other relevant intentions like letting other developers interract with your js source code in some sort.
However.
You should not use it to log very important messages as this could be a hole your application presumably.
Hope it helps.
While the console object is not defined in the official Javascript standard, it is specified in:
Google Chrome
Mozilla Firefox
Internet Explorer 9+
Opera
Safari
Node.js
PhantomJS (since it uses V8 like Chrome and Node.js)
and more, I'm sure. As long as you're debugging in any of the environments which supports it, you're fine. You should be removing your logging statements before pushing to production anyway so as long as it works for debugging, it's nothing to worry about.
Javascript on a page in Internet Explorer (8 and 9) with Developer Tools open reaches console.log(), which is defined - but nothing appears in the actual log.
Things tried:
Double-checking with alert()s that console is defined and that console.log is a function.
Checking with alert()s before and after that the code does reach the console.log() line,
Checking code for any IE fallbacks like if(ie){console={log:function(){}}}, removing links to libraries and checking code snippets for mentions of console
Checking IE's settings and enabling anything relating to debugging
Checking that IE isn't broken by loading a 3rd party page with console.log()s (e.g. http://jsbin.com logs "init" and "runner")
Swapping console for window.console
So the console is there and active, but no console messages show up in the actual console (on either the Script tab or the Console tab).
What else could stop console.log() from actually logging anything, even when Developer Tools is open and console.log is a defined function?
The culprit in this case turned out to be, of all things, firebug lite.
My test dev pages often include this to (ironically) aid certain types of debugging in IE:
<!--[if IE]>
<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js"></script>
<![endif]-->
Since it appears in text editors as a comment, it's easy to accidentally overlook.
Alerting alert(console.log); before and after shows firebug lite changes the console.log function from the native code to this:
function(){return f.apply(c,arguments)}
...which for some reason (at the moment, here) then does nothing.
Without Firebug Lite getting involved, alert(console.log); in IE gives this:
function log() {
[native code]
}
...and does its usual trick of logging if f12 Dev Tools is open and crashing if it isn't.
I've just read through some tutorials on using ajax with rails. Below you can see a JS script extracted from one of them with some modifications which I expected to cause some errors and write some text somewhere. (public/javascripts/application.js)
$('.submittable').live('change', function() {
$(this).parents('form:first').submit();
fdafdasfewa
document.write("Welcome to my world!!!");
echo "------------------";
});
In fact the script still works with no side effects.
Where does document.write and echo put text?
How can I debug such a script when I can't even see its output? Well sometimes probably I'll not even be able to determine if ran or not.
Try putting in an alert to make sure your code is being reached
$('.submittable').live('change', function() {
$(this).parents('form:first').submit();
alert('reached this point');
...
Also, use Firebug or Chrome's development tools. In Chrome on the mac the shortcut is command-alt-i to bring up the dev tools, then click 'console' to bring up...the console.
In the console you can type
$('.submittable')
To make sure your js has a dom element to attach to. If $('.submittable') returns nothing then there's no dom element selected. You can even set breakpoints and step through them in the dev tools. To create a break point just do
$('.submittable').live('change', function() {
$(this).parents('form:first').submit();
debugger;
...
and the dev tools will take over when that line is reached.
My guess is when you called the $(this).parents('form:first').submit() the page posted the form and javascript didn't execute past that point, which is why it didn't throw any errors of or write anything to the document.
I would use console.log to write test output in combination with the developer tools in chrome or Firebug. That way you know to look in the console and you can also use things like breakpoints to watch the execution of the javascript.
I'm working on a new project which has some complex javascript. I can't post any code so that's not what my question is about.
I have a script which works in Firefox 3.0. It was pointed out that the script did not work in Firefox 3.5, so I'm trying to make it work. Indeed the script didn't produce the expected results, so I installed the latest version of Firebug, enabled the console and refreshed the page.
And wow, it worked.
No errors, warnings nothing.
So I disabled the console, and then it didn't work anymore...
What's going on here? The Firebug console somehow changes something in Firefox that makes my script work? Any advice on what next? (besides asking future visitors to install Firebug...)
Could it be something as simple as forgetting to comment a call to console.log() somewhere in your javascript?
If you have hanging references, and the user doesn't have Firebug installed, you're going to get a runtime error that will halt execution of the script.
It sounds to me like there's a chance you have a threading problem, and FireBug is analyzing and possibly slowing down one of the threads so that it has time to complete before the next step is resolved.
Are you possibly utilizing ajax, and something is waiting on that response? Or possibly you're doing something on or after the load of an object that is depending on something else in the DOM?
UPDATE:
For those stumbling upon this now, "threads" in JavaScript really only exist in abstraction (web workers, etc). I was mis-using the term. I was really thinking of an asynchronous action that returned before another one was ready.
Check in your code for console.log(), console.debug().Calling window.console objects methods throws an error if console is undefined (as expected).
In most cases you can easily delete or comment that lines.
I wrote a simple wrapper for firebug (I just use debug but it should give you what you need to duplicate the other methods) that only writes when the console is there so I can use firebug, don't need to go comment out my debug statements and it doesn't break sites for people without it.
If you use this code then use fbconsole.debug instead of console.debug you will never have this problem:
function fbconsole () {
this.debug = function (val) {
if(typeof(console) !== 'undefined' && console != null) {
console.debug(val);
}
}
}
var fbconsole = new fbconsole();