Console API

From FirebugWiki

(Difference between revisions)
Jump to: navigation, search
m (Added a hint that the "console" object is a JavaScript variable)
 
(19 intermediate revisions not shown)
Line 1: Line 1:
-
<p>Firebug adds a global variable named "console" to all web pages loaded in Firefox.  This object contains many methods that allow you to write to the Firebug console to expose information that is flowing through your scripts.</p>
+
Firebug adds a global JavaScript variable named "console" to all web pages loaded in Firefox.  This object contains many methods that allow you to write to the Firebug console to expose information that is flowing through your scripts.
-
            <h3>console.log(object[, object, ...])</h3>
+
== Commands ==
-
            <p>Writes a message to the console.  You may pass as many arguments as you'd like, and they will be joined together in a space-delimited line.</p>
+
-
            <p>The first argument to <code>log</code> may be a string containing printf-like string substitution patterns.  For example:</p>
+
-
            <pre class="code">console.log("The %s jumped over %d tall buildings", animal, count);</pre>
+
-
            <p>The example above can be re-written without string substitution to achieve the same result:</p>
+
-
            <pre class="code">console.log("The", animal, "jumped over", count, "tall buildings");</pre>
+
=== [[console.log|console.log(object[, object, ...])]] ===
-
            <p>These two techniques can be combined. If you use string substitution but provide more arguments than there are substitution patterns, the remaining arguments will be appended in a space-delimited line, like so:</p>
+
Writes a message to the console. You may pass as many arguments as you'd like, and they will be joined together in a space-delimited line.
-
            <pre class="code">console.log("I am %s and I have:", myName, thing1, thing2, thing3);</pre>
+
-
            <p>If objects are logged, they will be written not as static text, but as interactive hyperlinks that can be clicked to inspect the object in Firebug's HTML, CSS, Script, or DOM tabs.  You may also use the %o pattern to substitute a hyperlink in a string.</p>
+
-
            <p>Here is the complete set of patterns that you may use for string substitution:</p>
+
=== [[console.debug|console.debug(object[, object, ...])]] ===
-
            <table width="600">
+
Writes a message to the console, including a hyperlink to the line where it was called.
-
                <tr><td class="keyHead" colspan="2">String Substitution Patterns</td></tr>
+
=== [[console.info|console.info(object[, object, ...])]] ===
-
                <tr><td class="keyType">%s</td><td class="keyCode">String</td></tr>
+
Writes a message to the console with the visual "info" icon and color coding and a hyperlink to the line where it was called.
-
                <tr><td class="keyType">%d, %i</td><td class="keyCode">Integer (numeric formatting is not yet supported)</td></tr>
+
-
                <tr><td class="keyType">%f</td><td class="keyCode">Floating point number (numeric formatting is not yet supported)</td></tr>
+
-
                <tr><td class="keyType">%o</td><td class="keyCode">Object hyperlink</td></tr>
+
-
            </table>
+
=== [[console.warn|console.warn(object[, object, ...])]] ===
 +
Writes a message to the console with the visual "warning" icon and color coding and a hyperlink to the line where it was called.
-
            <h3>console.debug(object[, object, ...])</h3>
+
=== [[console.error|console.error(object[, object, ...])]] ===
-
            <p>Writes a message to the console, including a hyperlink to the line where it was called.</p>
+
Writes a message to the console with the visual "error" icon and color coding and a hyperlink to the line where it was called.
-
            <h3>console.info(object[, object, ...])</h3>
+
=== [[console.assert|console.assert(expression[, object, ...])]] ===
-
            <p>Writes a message to the console with the visual "info" icon and color coding and a hyperlink to the line where it was called.</p>
+
Tests that an expression is true.  If not, it will write a message to the console and throw an exception.
-
            <h3>console.warn(object[, object, ...])</h3>
+
=== [[console.clear|console.clear()]] ===
-
            <p>Writes a message to the console with the visual "warning" icon and color coding and a hyperlink to the line where it was called.</p>
+
Clears the console.
-
            <h3>console.error(object[, object, ...])</h3>
+
=== [[console.dir|console.dir(object)]] ===
-
            <p>Writes a message to the console with the visual "error" icon and color coding and a hyperlink to the line where it was called.</p>
+
Prints an interactive listing of all properties of the object. This looks identical to the view that you would see in the [[DOM Panel]].
-
            <h3>console.assert(expression[, object, ...])</h3>
+
=== [[console.dirxml|console.dirxml(node)]] ===
 +
Prints the XML source tree of an HTML or XML element. This looks identical to the view that you would see in the [[HTML Panel]]. You can click on any node to inspect it in the [[HTML Panel]].
-
            <p>Tests that an expression is true. If not, it will write a message to the console and throw an exception.</p>
+
=== [[console.trace|console.trace()]] ===
 +
Prints an interactive stack trace of JavaScript execution at the point where it is called.
-
            <h3>console.dir(object)</h3>
+
The stack trace details the functions on the stack, as well as the values that were passed as arguments to each functionYou can click each function to take you to its source in the Script tab, and click each argument value to inspect it in the [[DOM Panel|DOM]] or [[HTML Panel]].
-
            <p>Prints an interactive listing of all properties of the objectThis looks identical to the view that you would see in the DOM tab.</p>
+
-
            <h3>console.dirxml(node)</h3>
+
=== [[console.group|console.group(object[, object, ...])]] ===
-
            <p>Prints the XML source tree of an HTML or XML element.  This looks identical to the view that you would see in the HTML tab.  You can click on any node to inspect it in the HTML tab.</p>
+
Writes a message to the console and opens a nested block to indent all future messages sent to the console. Call <code>console.groupEnd()</code> to close the block.
-
            <h3>console.trace()</h3>
+
=== [[console.groupCollapsed|console.groupCollapsed(object[, object, ...])]] ===
-
            <p>Prints an interactive stack trace of JavaScript execution at the point where it is called.</p>
+
Like <code>console.group()</code>, but the block is initially collapsed.
-
            <p>The stack trace details the functions on the stack, as well as the values that were passed as arguments to each function. You can click each function to take you to its source in the Script tab, and click each argument value to inspect it in the DOM or HTML tabs.</p>
+
-
            <h3>console.group(object[, object, ...])</h3>
+
=== [[console.groupEnd|console.groupEnd()]] ===
-
            <p>Writes a message to the console and opens a nested block to indent all future messages sent to the console.  Call <code>console.groupEnd()</code> to close the block.</p>
+
Closes the most recently opened block created by a call to <code>console.group()</code> or <code>console.groupCollapsed()</code>
-
            <h3>console.groupCollapsed(object[, object, ...])</h3>
+
=== [[console.time|console.time(name)]] ===
-
            <p>Like <code>console.group()</code>, but the block is initially collapsed.</p>
+
Creates a new timer under the given name. Call <code>[[console.timeEnd|console.timeEnd()]]</code> with the same name to stop the timer and print the time elapsed.
-
            <h3>console.groupEnd()</h3>
+
=== [[console.timeEnd|console.timeEnd(name)]] ===
-
            <p>Closes the most recently opened block created by a call to <code>console.group()</code> or <code>console.groupEnd()</code></p>
+
Stops a timer created by a call to <code>console.time(name)</code> and writes the time elapsed.
-
            <h3>console.time(name)</h3>
+
=== [[console.timeStamp|console.timeStamp(name)]] ===
-
            <p>Creates a new timer under the given name.  Call <code>console.timeEnd(name)</code> with the same name to stop the timer and print the time elapsed..</p>
+
Creates a time stamp, which can be used together with [[Net Panel#Request Timeline|HTTP traffic timing]] to measure when a certain piece of code was executed.
-
            <h3>console.timeEnd(name)</h3>
+
=== [[console.profile|console.profile([title])]] ===
-
            <p>Stops a timer created by a call to <code>console.time(name)</code> and writes the time elapsed.</p>
+
Turns on the JavaScript profiler.
-
            <h3>console.profile([title])</h3>
+
=== [[console.profileEnd|console.profileEnd()]] ===
-
            <p>Turns on the JavaScript profiler.  The optional argument <code>title</code> would contain the text to be printed in the header of the profile report.</p>
+
Turns off the JavaScript profiler and prints its report.
-
            <h3>console.profileEnd()</h3>
+
=== [[console.count|console.count([title])]] ===
-
            <p>Turns off the JavaScript profiler and prints its report.</p>
+
Writes the number of times that the line of code where <code>count</code> was called was executed.
-
            <h3>console.count([title])</h3>
+
=== [[console.exception|console.exception(error-object[, object, ...])]] ===
-
            <p>Writes the number of times that the line of code where <code>count</code> was called was executed.  The optional argument <code>title</code> will print a message in addition to the number of the count.</p>
+
Prints an error message together with an interactive stack trace of JavaScript execution at the point where the exception occurred.
-
            <h3>console.exception([error-object])</h3>
+
=== [[console.table|console.table(data[, columns])]] ===
-
            <p>Prints an error message together with an interactive stack trace of JavaScript execution at the point where the exception occurred.</p>
+
Allows to log provided data using tabular layout.
-
           
+
-
            <h2>Implementation Notes</h2>
+
-
            <p>The <code>console</code> is an object attached to the <code>window</code> object in the web page.  
+
== Implementation Notes ==
-
            In Firebug for Firefox the object is attached only if the Console panel is enabled. In Firebug lite,
+
* The <code>console</code> is an object attached to the <code>window</code> object in the web page.
-
            the console is attached if Lite is installed in the page.</p>
+
* If the page already contains a <code>console</code> variable, it is '''not''' overwritten by Firebug's Console API.
-
            <h3>Firebug 1.4</h3>
+
* In Firebug for Firefox the object is attached only if the [[Console Panel]] is enabled, and it is injected just before the first JavaScript script tag is run. So the first script tag is compiled, then the console is injected, then the outer function code of the script tag is executed. As a consequence, Firebug might not always catch logs done by e.g. Greasemonkey scripts. [http://code.google.com/p/fbug/issues/detail?id=4978 This might be fixed in a future version.]
-
            <p>The console is implemented by adding a <code>div</code> element and a <code>script</code> tag to the web page just
+
* In [http://getfirebug.com/firebuglite Firebug Lite], the console is attached as soon as Lite is installed into the page.
-
            before the first Javascript script tag is run. So the first script tag is compiled, then the console is injected, then
+
-
            the outer function code of the script tag is executed.</p>
+
-
            <h3>Firebug 1.3</h3>
+
== Note ==
-
            <p>As in Firebug 1.4</p>
+
The console API formerly implemented a <code>console.firebug</code> property. This property was removed from the API in Firebug 1.9.0 in order to prevent sites from detecting whether a user has Firebug installed.
-
            <h3>Firebug 1.2</h3>
+
 
-
            <p>The code and tags are added on document load event.</p>
+
== See also ==
-
            <h3>Firebug 1.1 and earlier</h3>
+
* [[Console]]
-
            <p>The console is implemented with an insecure technique</p>
+
* [[Command Line API]]
 +
 
 +
[[Category:Firebug]]
 +
[[Category:UI]]
 +
[[Category:API]]

Latest revision as of 13:48, 17 February 2014

Firebug adds a global JavaScript variable named "console" to all web pages loaded in Firefox. This object contains many methods that allow you to write to the Firebug console to expose information that is flowing through your scripts.

Contents

[edit] Commands

[edit] console.log(object[, object, ...])

Writes a message to the console. You may pass as many arguments as you'd like, and they will be joined together in a space-delimited line.

[edit] console.debug(object[, object, ...])

Writes a message to the console, including a hyperlink to the line where it was called.

[edit] console.info(object[, object, ...])

Writes a message to the console with the visual "info" icon and color coding and a hyperlink to the line where it was called.

[edit] console.warn(object[, object, ...])

Writes a message to the console with the visual "warning" icon and color coding and a hyperlink to the line where it was called.

[edit] console.error(object[, object, ...])

Writes a message to the console with the visual "error" icon and color coding and a hyperlink to the line where it was called.

[edit] console.assert(expression[, object, ...])

Tests that an expression is true. If not, it will write a message to the console and throw an exception.

[edit] console.clear()

Clears the console.

[edit] console.dir(object)

Prints an interactive listing of all properties of the object. This looks identical to the view that you would see in the DOM Panel.

[edit] console.dirxml(node)

Prints the XML source tree of an HTML or XML element. This looks identical to the view that you would see in the HTML Panel. You can click on any node to inspect it in the HTML Panel.

[edit] console.trace()

Prints an interactive stack trace of JavaScript execution at the point where it is called.

The stack trace details the functions on the stack, as well as the values that were passed as arguments to each function. You can click each function to take you to its source in the Script tab, and click each argument value to inspect it in the DOM or HTML Panel.

[edit] console.group(object[, object, ...])

Writes a message to the console and opens a nested block to indent all future messages sent to the console. Call console.groupEnd() to close the block.

[edit] console.groupCollapsed(object[, object, ...])

Like console.group(), but the block is initially collapsed.

[edit] console.groupEnd()

Closes the most recently opened block created by a call to console.group() or console.groupCollapsed()

[edit] console.time(name)

Creates a new timer under the given name. Call console.timeEnd() with the same name to stop the timer and print the time elapsed.

[edit] console.timeEnd(name)

Stops a timer created by a call to console.time(name) and writes the time elapsed.

[edit] console.timeStamp(name)

Creates a time stamp, which can be used together with HTTP traffic timing to measure when a certain piece of code was executed.

[edit] console.profile([title])

Turns on the JavaScript profiler.

[edit] console.profileEnd()

Turns off the JavaScript profiler and prints its report.

[edit] console.count([title])

Writes the number of times that the line of code where count was called was executed.

[edit] console.exception(error-object[, object, ...])

Prints an error message together with an interactive stack trace of JavaScript execution at the point where the exception occurred.

[edit] console.table(data[, columns])

Allows to log provided data using tabular layout.

[edit] Implementation Notes

  • The console is an object attached to the window object in the web page.
  • If the page already contains a console variable, it is not overwritten by Firebug's Console API.
  • In Firebug for Firefox the object is attached only if the Console Panel is enabled, and it is injected just before the first JavaScript script tag is run. So the first script tag is compiled, then the console is injected, then the outer function code of the script tag is executed. As a consequence, Firebug might not always catch logs done by e.g. Greasemonkey scripts. This might be fixed in a future version.
  • In Firebug Lite, the console is attached as soon as Lite is installed into the page.

[edit] Note

The console API formerly implemented a console.firebug property. This property was removed from the API in Firebug 1.9.0 in order to prevent sites from detecting whether a user has Firebug installed.

[edit] See also

Personal tools