Console Object
console
is effectively an object with several associated methods.
The console
object provides access to the browser debug console. The operation of this object varies from browser to browser but there are certain methods that are seen as a standard. One of these methods is log()
.
Log method
The log()
method exists essentially to allow data to be sent to the browser debug console. Any information can be sent, usually for debugging code.
Example of the Firefox console:
Clickctrl+shift+ktocallitinFirefox.
Considerations
Asthisobjectvariesandthereisstillnostandardtogovernitsoperationandmethodology,therearesomeconsiderationstohave:
// Exemplo a enviar algo para a consola do navegador
var s = "bubu";
console.log(s);
Potential problems:
- The user may have turned off the console;
- The browser may not have a Web console;
- The method we are using may not work in a given browser.
In JavaScript, a runtime failure can result in a blocking of all other JavaScript. If for example we use console.log(X)
when the console does not exist or the log()
method does not exist, we will have an error in our code.
Alternative:
We can make use of a try...catch
to try to send to the console and in case of failure to act otherwise:
function consola(s) {
try { console.log(s); } catch (e) { alert(s); }
}
And so if it goes well, the information will be in the console, if it goes bad it will be emitted a alert()
with the information that would go to the console.
Of course we can do nothing or send an Ajax call to a server with the error if it is critical and has to be registered "give to anyone who hurts."
/* Enviar algo para a consola do navegador com fallback
* fazendo uso da função já declarada conforme visto em cima
*/
var s = "bubu";
consola(s);
Consoles
As explained, there is not really a standard for the web console, so there are some references to the help pages for each one:
Note: Response to evolve within a few days as you finish compiling more relevant information on this subject.