The Console

The console offers a way to inspect and debug your webpages. Think of it as the Terminal of your web content. The console has access to the DOM and JavaScript of the open page. Use the console as a tool to modify your web content via interactive commands and as a teaching aid to expand your knowledge of JavaScript. Because an object’s methods and properties autocomplete as you type, you can see all available functions that are valid in Safari.

For example, open the console and type $$(‘p’)[1]. ($$ is shorthand for document.querySelectorAll—see more shorthand commands in Table 5-1.) Because this paragraph is the second instance of the p element on this page ([1] in a 0-based index), the node represents this paragraph. As you hover over the node, its position on the page is visibly highlighted. You can expand the node to see its contents, and even press Command-C to copy it to your clipboard.

Command-Line API

You can inspect HTML nodes and JavaScript objects in more detail by using the console commands listed in Table 5-1. Type the command-line APIs interactively within the console.

If your scripts share the same function name as a Command-Line API function, the function in your scripts takes precedence.

Table 5-1  Commands available in the Web Inspector console

Command

Description

$(selector)

Shorthand for document.querySelector.

$$(selector)

Shorthand for document.querySelectorAll.

$x(xpath)

Returns an array of elements that match the given XPath expression.

$0

Represents the currently selected node in the content browser.

$1..4

Represents the last, second to last, third to last, and fourth to last selected node in the content browser, respectively.

$_

Returns the value of the last evaluated expression.

dir(object)

Prints all the properties of the object.

dirxml(object)

Prints all the properties of the object. If the object is a node, prints the node and all child nodes.

keys(object)

Prints an array of the names of the object’s own properties.

values(object)

Prints an array of the values of the object’s own properties.

profile([title])

Starts the JavaScript profiler. The optional argument title contains the string to be printed in the header of the profile report. See JavaScript and Events Recording.

profileEnd()

Stops the JavaScript profiler and prints its report. See JavaScript and Events Recording.

getEventListeners(object)

Prints an object containing the object’s attached event listeners.

monitorEvents(object[, types])

Starts logging all events dispatched to the given object. The optional argument types defines specific events or event types to log, such as “click”.

unmonitorEvents(object[, types])

Stops logging for all events dispatched to the given object. The optional argument types defines specific events or event types to stop logging, such as “click”.

inspect(object)

Inspects the given object; this is the same as clicking the Inspect button.

copy(object)

Copies the given object to the clipboard.

clear()

Clears the console.

The functions listed in Table 5-1 are regular JavaScript functions that are part of the Web Inspector environment. That means you can use them as you would any JavaScript function. For example, you can assign a chain of Console API commands to a variable to create a useful shorthand. Listing 5-1 shows how you can quickly see all event types attached to the selected node.

Listing 5-1  Find the events attached to this element

var evs = function () {
    return keys(getEventListeners($0));
};

After defining this function, inspect the magnifying glass in the top-right corner of this webpage, and type evs() in the console. An array containing the string “click” is returned, because there is a click event listener attached to that element.

Of course, these functions shouldn’t be included in your website’s JavaScript files because they are not available in the browser environment. Only use these functions in the Web Inspector console. Console functions you can include in your scripts are described in Console API.

Console API

You can output messages to the console, add markers to the timeline, and control the debugger directly from your scripts by using the commands listed in Table 5-2.

Table 5-2  JavaScript functions available in the Console API

Function

Description

console.assert(expression, object)

Asserts whether the given expression is true. If the assertion fails, prints the error and increments the number of errors in the activity viewer. If the assertion succeeds, prints nothing.

console.clear()

Clears the console.

console.count([title])

Prints the number of times this line has been called.

console.debug(object)

Alias of console.log().

console.dir(object)

Prints the properties and values of the object.

console.dirxml(node)

Prints the DOM tree of an HTML or XML node.

console.error(object)

Prints a message to the console with the error icon. Increments the number of errors shown in the activity viewer.

console.group([title])

Prints subsequent logs under a disclosure of the given title.

console.groupEnd()

Ends the previously declared console grouping.

console.info(object)

Alias of console.log().

console.log(object)

Prints the object to the console with the log icon. Increments the number of logs shown in the activity viewer.

console.markTimeline(label)

Marks the Timeline with a green vertical dashed line that indicates when this line of code was called. See Recording Timelines.

console.profile([title])

Starts the JavaScript profiler. The optional argument title contains the string to be printed in the header of the profile report. See JavaScript and Events Recording.

console.profileEnd([title])

Stops the JavaScript profiler and prints its report. See JavaScript and Events Recording.

console.time(name)

Starts a timer associated with the given name. Useful for timing the duration of segments of code.

console.timeEnd(name)

Stops the timer associated with the given name and prints the elapsed time to the console.

console.trace()

Prints a stack trace at the moment the function is called. See Figure 4-2.

console.warn(object)

Prints a message to the console with the warning icon. Increments the number of warnings shown in the activity viewer.

debugger

Stops JavaScript execution at the current line. This is the equivalent of setting a breakpoint programmatically. See Breakpoints.