Web Console Helpers

  • Revision slug: Tools/Web_Console/Helpers
  • Revision title: Web Console Helpers
  • Revision id: 47178
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment new page, content migrated from parent page; page created, 347 words added

Revision Content

{{ gecko_minversion_header("2.0") }}

The JavaScript command line provided by the Web Console offers a few built-in helper functions that make certain tasks easier.

$()
Looks up a string as an ID of a DOM node; this is a shortcut for {{ domxref("document.getElementById()") }}.
$$()
Looks up a CSS selector string, returning a list of DOM nodes that match. This is a shortcut for {{ domxref("document.querySelectorAll()") }}.
keys()
Given an object, returns a list of the keys (or property names) on that object. This is a shortcut for object.keys.
values()
Given an object, returns a list of the values on that object; serves as a companion to keys().
clear()
Clears the console output area.
inspect()
Given an object, opens the object inspector for that object.
pprint()
Formats the specified value in a readable way; this is useful for dumping the contents of objects and arrays.

Example: Looking at the contents of a DOM node

Let's say you have a DOM node with the ID "header". In fact, this page you're reading right now has one, so you can open up the Web Console and try this right now.

Let's take a look at the contents of that node by using the $() and inspect() functions:

inspect($("header"))

This automatically opens up the object inspector, showing you the contents of the DOM node with the ID "header".

Example: Dumping the contents of a DOM node

That's well and good if you happen to be sitting at the browser exhibiting some problem, but let's say you're debugging remotely for a user, and need a look at the contents of a node. You can have your user open up the Web Console and dump the contents of the node into the log, then copy and paste it into an email to you, using the pprint() function:

pprint($("header"))

This spews out the contents of the node so you can take a look. Of course, this may be more useful with other objects than a DOM node, but you get the idea.

See also

Revision Source

<p>{{ gecko_minversion_header("2.0") }}</p>
<p>The JavaScript command line provided by the Web Console offers a few built-in helper functions that make certain tasks easier.</p>
<dl> <dt><code>$()</code></dt> <dd>Looks up a string as an ID of a DOM node; this is a shortcut for {{ domxref("document.getElementById()") }}.</dd> <dt><code>$$()</code></dt> <dd>Looks up a CSS selector string, returning a list of DOM nodes that match. This is a shortcut for {{ domxref("document.querySelectorAll()") }}.</dd> <dt><code>keys()</code></dt> <dd>Given an object, returns a list of the keys (or property names) on that object. This is a shortcut for <code>object.keys</code>.</dd> <dt><code>values()</code></dt> <dd>Given an object, returns a list of the values on that object; serves as a companion to <code>keys()</code>.</dd> <dt><code>clear()</code></dt> <dd>Clears the console output area.</dd> <dt><code>inspect()</code></dt> <dd>Given an object, opens the object inspector for that object.</dd> <dt><code>pprint()</code></dt> <dd>Formats the specified value in a readable way; this is useful for dumping the contents of objects and arrays.</dd>
</dl>
<h4>Example: Looking at the contents of a DOM node</h4>
<p>Let's say you have a DOM node with the ID "header". In fact, this page you're reading right now has one, so you can open up the Web Console and try this right now.</p>
<p>Let's take a look at the contents of that node by using the <code>$()</code> and <code>inspect()</code> functions:</p>
<p><code>inspect($("header"))</code></p>
<p>This automatically opens up the object inspector, showing you the contents of the DOM node with the ID "header".</p>
<h4>Example: Dumping the contents of a DOM node</h4>
<p>That's well and good if you happen to be sitting at the browser exhibiting some problem, but let's say you're debugging remotely for a user, and need a look at the contents of a node. You can have your user open up the Web Console and dump the contents of the node into the log, then copy and paste it into an email to you, using the <code>pprint()</code> function:</p>
<pre>pprint($("header"))
</pre>
<p>This spews out the contents of the node so you can take a look. Of course, this may be more useful with other objects than a DOM node, but you get the idea.</p>
<h2>See also</h2>
<ul> <li><a href="/en/Using_the_Web_Console" title="en/Using the Web Console">Using the Web Console</a></li>
</ul>
Revert to this revision