index.rst (7898B)
1 ================================ 2 The JavaScript input interpreter 3 ================================ 4 5 You can interpret JavaScript expressions in real time using the interpreter provided by the Web Console. It has two modes: single-line entry and multi-line entry. 6 7 Single-line mode 8 **************** 9 10 For single-line entry, you can type JavaScript expressions in the field at the bottom of the console log, at the **>>** prompt. 11 12 .. image:: web_console_single.png 13 :alt: The Web Console, showing single-line mode 14 :class: center 15 16 17 To enter expressions in single-line mode, type at the prompt and press :kbd:`Enter`. To enter multi-line expressions, press :kbd:`Shift` + :kbd:`Enter` after typing each line, then :kbd:`Enter` to run all the entered lines. 18 19 The expression you type is echoed under the input prompt, followed by the result. 20 21 If your input does not appear to be complete when you press :kbd:`Enter`, then the Console treats this as :kbd:`Shift` + :kbd:`Enter` , enabling you to finish your input. 22 23 For example, if you type: 24 25 .. code-block:: JavaScript 26 27 function foo() { 28 29 30 and then :kbd:`Enter`, the Console does not immediately execute the input, but behaves as if you had pressed :kbd:`Shift` + :kbd:`Enter` , so you can finish entering the function definition. 31 32 33 .. _command_line_interpreter_multi_line_mode: 34 35 Multi-line mode 36 *************** 37 38 For multi-line entry, click the "split panel" icon at the right hand side of the single-line entry field, or press :kbd:`Ctrl` + :kbd:`B` (Windows/Linux) or :kbd:`Cmd` + :kbd:`B` (macOS). The multi-line editing pane opens on the left side the of Web Console. 39 40 .. image:: web_console_multi.png 41 :alt: Web Console in multi-line mode 42 :class: center 43 44 You can enter multiple lines of JavaScript by default in this mode, pressing :kbd:`Enter` after each one. To execute the snippet that is currently in the editing pane, click the **Run** button or press :kbd:`Ctrl` + :kbd:`Enter` (or :kbd:`Cmd` + :kbd:`Return` on MacOS). The snippet is echoed under the input prompt (in the right-side pane), followed by the result. 45 46 If the code snippet is more than five lines long, only the first five lines are echoed in the console, preceded by a disclosure triangle (or "twistie"), and followed by an ellipsis (…). Click anywhere in the area containing the echoed code to show the whole snippet; click again in that area to collapse it. 47 48 You can open files when in multi-line mode, and save the current contents of the editing pane to a file. 49 50 51 - To open a file, press :kbd:`Ctrl` + :kbd:`O` (:kbd:`Cmd` + :kbd:`O` on MacOS). A file dialog box opens so you can select the file to open. 52 - To save the contents of the editing pane, press :kbd:`Ctrl` + :kbd:`S` (:kbd:`Cmd` + :kbd:`S` on MacOS). A file dialog box opens so you can specify the location to save to. 53 54 55 To switch back to single-line mode, click the **X** icon at the top of the multi-line editing pane, or press :kbd:`Ctrl` + :kbd:`B` (Windows/Linux) or :kbd:`Cmd` + :kbd:`B` (MacOS). 56 57 58 Accessing variables 59 ******************* 60 61 You can access variables defined in the page, both built-in variables like ``window`` and variables added by JavaScript libraries like *jQuery*: 62 63 .. image:: commandline-accessbuiltin.png 64 :class: center 65 66 .. image:: commandline-accesspageadded.png 67 :class: center 68 69 70 .. _command_line_interpreter_autocomplete: 71 72 Autocomplete 73 ************ 74 75 The editor has autocomplete: enter the first few letters and a popup appears with possible completions: 76 77 .. image:: console_autocomplete_cropped.png 78 :class: center 79 80 81 Press :kbd:`Enter`, :kbd:`Tab`, or the right arrow key 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. 82 83 Console autocomplete suggestions are case-insensitive. 84 85 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. 86 87 You get autocomplete suggestions for array elements, as well: 88 89 .. image:: arraylist_autocomplete.png 90 :class: border 91 92 93 You can enable or disable autocompletion via the Settings ("gear") menu in the Web Console toolbar. The menuitem **Enable Autocompletion** has a checkmark next to it when the feature is enabled, which is missing when it is disabled. Select the menuitem to change the state. 94 95 96 Instant evaluation 97 ****************** 98 99 .. note:: 100 This feature is available in Firefox Nightly, in versions labeled 74 and later. 101 102 When the "instant evaluation" feature is enabled, the interpreter displays results of expressions as you're typing them in single-line mode. Note that the result might be an error message. Expressions that have side effects are not evaluated. 103 104 You can enable or disable instant evaluation via the Settings ("gear") menu in the Web Console toolbar. The menuitem **Instant Evaluation** has a checkmark next to it when the feature is enabled, which is missing when it is disabled. Select the menuitem to change the state. 105 106 107 Execution context 108 ***************** 109 110 Code that you have executed becomes part of the execution context, regardless of what editing mode you were in when you executed it. For example, if you type a function definition in the multi-line editor, and click **Run**, you can switch to single-line mode and still use your function. 111 112 113 Syntax highlighting 114 ******************* 115 116 .. image:: console_syntaxhighlighting.png 117 :alt: Console output showing syntax highlighting 118 :class: border 119 120 121 The text you enter has syntax highlighting as soon as you have typed enough for the highlighter to parse it and infer the meanings of the "words". 122 123 The output is highlighted as well where appropriate. 124 125 .. note:: 126 Syntax highlighting is not visible in your browser if Accessibility features have been enabled. 127 128 129 .. _command_line_interpreter_execution_history: 130 131 Execution history 132 ***************** 133 134 The interpreter remembers expressions you've typed. To move back and forward through your history: 135 136 137 - In single-line mode, use the up and down arrows. 138 - In multi-line mode, use the **⋀** and **⋁** icons in the editing panel's toolbar. 139 140 141 The expression history is persisted across sessions. To clear the history, use the ``clearHistory()`` :ref:`helper function <command_line_interpreter_helper_commands>`. 142 143 You can initiate a reverse search through the expression history, much like you can in bash on Linux and Mac or PowerShell on Windows. On Windows and Linux press :kbd:`F9`. On Mac press :kbd:`Ctrl` + :kbd:`R` (**note:** not :kbd:`Cmd` + :kbd:`R`!) to initiate the reverse search. 144 145 .. image:: reverse_search.png 146 :class: border 147 148 149 Enter the text you want to search for in the input box at the bottom of the Console. Start typing part of the expression you are looking for and the first match is displayed in the console. Repeatedly typing :kbd:`F9` on Windows and Linux ( :kbd:`Ctrl` + :kbd:`R` on Mac) cycles backwards through the matches. 150 151 .. image:: reverse_search_example.png 152 :class: border 153 154 Once you have initiated the reverse search, you can use :kbd:`Shift` + :kbd:`F9` on Windows or Linux ( :kbd:`Ctrl` + :kbd:`S` on Mac) to search forward in the list of matches. You can also use the **⋀** and **⋁** icons in the expression search bar. 155 156 When you find the expression you want, press :kbd:`Enter` (:kbd:`Return`) to execute the statement. 157 158 159 Working with iframes 160 ******************** 161 162 :doc:`Working with iframes <../../working_with_iframes/index>` explains how to direct all debugging tools to target a particular iframe, including the command line interpreter. 163 164 165 .. _command_line_interpreter_helper_commands: 166 167 Helper commands 168 *************** 169 170 The JavaScript command line provided by the Web Console offers a few built-in helper functions that make certain tasks easier. For more information see :doc:`Web Console Helpers <../helpers/index>`.