正在翻譯中。
You can interpret JavaScript expressions in real time using the command line provided by the Web Console.
Entering expressions
To enter expressions just type into the command line and press Enter. To enter multiline expressions, use Shift+Enter instead of Enter.
The expression you type is echoed in the message display window, followed by the result:
From Firefox 47 onwards, if your input does not appear to be complete when you press Enter, then the Console treats this as Shift+Enter , enabling you to finish your input.
For example, if you type:
function foo() {
and then Enter, the Console will not immediately execute the input, but will behave as if you had pressed Shift+Enter , so you can finish entering the function definition.
Accessing variables
You can access variables defined in the page, both built-in variables like window
and variables added by JavaScript like jQuery
:
Autocomplete
The command line has autocomplete: enter the first few letters and a popup appears with possible completions:
Type Enter or Tab to accept the suggestion, use the up/down arrows to move to a different suggestion, or just keep typing if you don't like any of the suggestions.
The console suggests completions from the scope of the currently executing stack frame. This means that if you've hit a breakpoint in a function you get autocomplete for objects local to the function.
You get autocomplete suggestions for array elements, as well:
Defining variables
You can define your own variables, and then access them:
Command history
The command line remembers commands you've typed: to move back and forward through your history, use the up and down arrows.
From Firefox 39 onwards, this history is persisted across sessions. To clear the history, use the clearHistory()
helper function.
Working with iframes
If a page contains embedded iframes, you can use the cd()
command to change the console's scope to a specific iframe, and then you can execute functions defined in the document hosted by that iframe. There are three ways to select an iframe using cd()
:
You can pass the iframe DOM element:
var frame = document.getElementById("frame1"); cd(frame);
You can pass a CSS selector that matches the iframe:
cd("#frame1");
You can pass the iframe's global window object:
var frame = document.getElementById("frame1"); cd(frame.contentWindow);
To switch the context back to the top-level window, call cd()
with no arguments:
cd();
For example, suppose we have a document that embeds an iframe:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <iframe id="frame1" src="static/frame/my-frame1.html"></iframe> </body> </html>
The iframe defines a new function:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script> function whoAreYou() { return "I'm frame1"; } </script> </head> <body> </body> </html>
You can switch context to the iframe like this:
cd("#frame1");
Now you'll see that the global window's document is the iframe:
And you can call the function defined in the iframe:
Helper commands
The JavaScript command line provided by the Web Console offers a few built-in helper functions that make certain tasks easier.
$()
- Looks up a CSS selector string, returning the first element that matches. Equivalent to
document.querySelector()
or calls the $ function in the page, if it exists. $$()
- Looks up a CSS selector string, returning an array of DOM nodes that match. This is like for
document.querySelectorAll()
, but returns an array instead of aNodeList
. $0
- The currently-inspected element in the page.
$_
- Stores the result of the last expression executed in the console's command line. For example, if you type "2+2 <enter>", then "$_ <enter>", the console will print 4.
$x()
- Evaluates an XPath expression and returns an array of matching nodes.
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.
help()
- Displays help text. Actually, in a delightful example of recursion, it will bring you to this page.
cd()
-
Switch JavaScript evaluation context to a different iframe in the page. This helper accepts multiple different ways of identifying the frame to switch to. You can supply any of the following:
- a selector string that will be passed to
document.querySelector
to locate the iframe element - the iframe element itself
- the content window inside the iframe
See working with iframes.
- a selector string that will be passed to
copy()
- New in Firefox 38. Copy the argument to the clipboard. If the argument is a string, it's copied as-is. If the argument is a DOM node, its
outerHTML
is copied. Otherwise,JSON.stringify
will be called on the argument, and the result will be copied to the clipboard. clearHistory()
- New in Firefox 39. Just like a normal command line, the console command line remembers the commands you've typed. Use this function to clear the console's command history.
Please refer to the Console API for more information about logging from content.