Thursday, 31 May 2012

Chrome Developer Tools: Console


Google Chrome Dev Tools: How to use the Console Panel


Continuing with Google Chrome Developers Tools, this piece will demonstrate using the Console Panel to conduct debugging, inspect the DOM, or analyze any HTML errors for selected web pages. In previous posts on the Google Development toolkit, I introduced the Elements Panel and the Resource Panel.
In Google Chrome, select any page you wish to inspect; for this example, I will be demonstrating the Console using the Wikipedia.org website.
There are several ways to open up the Google Web Developer Tools; the easiest way is to just right-click on any page element and select Inspect element, resulting in the display as shown below for the Wikipedia.org site; this is the docked view. If you wish to undock the tool, click the dock/undock icon at the bottom left of the tool (see Figure B).

Figure B

Click images to enlarge.
Next, click the show Console button on the far right side of the toolbar. If there are any errors or warnings detected, they will be displayed in the bottom-right corner of the window. Clicking on any of these errors will also open the Console panel.
The Console provides auto-completion and tab-completion; therefore, as you type expressions, property names are automatically suggested. If there are multiple properties with the same prefix, pressing the Tab key will allow you to cycle through the list. Pressing the right arrow key will accept the highlighted suggestion. The current suggestion is also accepted by pressing the Tab key if there is only one matched property. The Console panel supports all 21 functions provided inFirebug’s Command Line API. Figure C shows the Console panel un-docked below.

Figure C

Command Line API is particularly useful when working with the Elements panel in conjunction with the Console panel. While in the Elements panel, open console by pressing the Esc key, as shown in Figure D.

Figure D

The following examples will demonstrate returning a single element with a given id using the command line function $(id). At the Console prompt, type $(”bodyContent”) and press enter. The command dumps the node with the given id — in this case, bodyContent — and displays it in the Console panel, as displayed in Figure E:
Using the command line function $0 returns an array of elements that match the given XPath expression. For example typing $0 at the Console prompt will dump the most recently selected node into the console, as displayed in Figure F:

The command line function $1 returns the currently-selected object in the Console panel, and $nwill display previously selected nodes.
Inspecting an object in the most suitable tab, or the tab identified by the optional argumenttabName can be accomplished by using the function inspect (object[, tabName]); the available optional tab names are “html”, “css”, “script”, and “dom”. For example, at the console prompt, type in inspect (searchform) and press enter; the selected object with the given id is displayed as shown in Figure G:
The dir (object) function prints an interactive listing of all properties of the specified object, and will appear identical to the view that you would see in selecting a DOM tab. In this example, at the console prompt type in dir (searchform) and press enter, the resulting display dumps the object with the given id, and as a JavaScript object with its associated properties, and is displayed inFigure H:
The dirxml (node) function displays the XML source tree of an HTML or XML element. In this example, type dirxml (searchform) into the Console prompt and click return; the resulting dump displays the object of the given id as an HTML sub-tree, displayed in Figure I:

No comments:

Post a Comment