IE debugging page 3: Use the F12 tool console to view errors and status (Windows)

Source: Internet
Author: User

You can use the F12 console command to receive error messages from Windows Internet Explorer 9 andCodeSend back your own message without interrupting the execution flow.

You can use the F12 tool console view inProgramRun the script statement immediately outside of the Code.

Console tabs and views

You can view messages on the F12 tool console on the console tab or in the console pane under the script tab. The console receives a message from Windows Internet Explorer when it is opened (for example, when the Code contains an error ). Internet Explorer 9 can send many message and error messages to the console. To navigate to an error location in the code, click the source information provided in the error. If a message is displayed when the F12 tool is disabled, a warning message is displayed the next time you enable the F12 tool. The F12 tool console is displayed on the following screen.

You can also send messages from the code to the console to record status, Mark errors, or warn about problems related to using console objects. Internet Explorer 9 provides four types of messages to differentiate problems in code-logs, warnings, errors, and information. These messages can be used during debugging without "window. Alert ()", or only run logs are retained (if important declarations are made in the Code ). A message string can contain text, variables, expression results, or a combination of all the content. The following screen shows the F12 tool console with multiple messages displayed.

Send messages from code to the console

The F12 tool provides commands that can be used from script code to send messages, start or stop analysis, or change the window for evaluating and typing script statements on the console.

Use console objects to send messages from code to the console. When testing code, you can use the console instead of "window. Alert ()". This is not so obvious and will not stop execution through the mode dialog box. This object provides a large number of forms to distinguish between information messages and error messages as needed. When using console objects, make sure that the F12 tool is enabled. To avoid unnecessary code execution, use the following function to test:

 
If(Window. Console & window. Console. Clear)

When you test a large number of Internet Explorer 9 objects that do not have parentheses or parameters, if a feature exists, the return value is true. In this case, we will test the console. Clear () function. You can also perform a general check by testing only console objects:

 
If(Window. Console ){//Add console commands here.}

Window. Console

The following table shows the syntax and example of console commands that can be used in scripts.

Command Example Description
Log (Message)
Window. Console. Log ("this is a logging message ");
Print "message" to the console, starting with "log.
Warn (Message)
Window. Console. Warn ("this is a warning message ");
Print the warning "message" to the console ". The message starts with a warning icon.
Error (Message)
Window. Console. Error ("this is an error message ");
Print the error "message" to the console ". The message text is displayed in red and starts with the error icon.
Info (Message)
W.w.le.info ("this is an info message ");
Print Information "message" to the console ". This message starts with an information icon.
Clear ()
Window. Console. Clear ();
Clear messages in the console. Do not clear the script error message or script code you entered in the console command line. Right-click the "console" pane and click the "Clear console" option to clear all messages.
Dir (object)
Window. Console. dir (oexample );
Print "object" attributes to the console.
Assert (expression, message)
Window. Console. Assert (x = 1), "assert message: X! = 1 ");
Print "message" (if the value of "expression" is false ).
Profile (report)
Window. Console. Profile ("My profile Report ");
Start recording the configuration file information under the title of "report. This command is equivalent to clicking the start sampling button on the configuration file tab.
Profileend ()
Window. Console. profileend ();
Stop recording configuration file information under the title of the previous report. This command is equivalent to clicking the stop sampling button on the configuration file tab. You can view the "Report" on the "configuration files" tab """.

You can use the alternative mode of the "printf" style to set the format of the message console command. For example, you can use one of the following methods to call "console. log ":

 
Console. Log ("variable X =" + x + "and variable Y =" +Y) console. Log ("Variable X =", X, "and variable Y =", Y) console. Log ("Variable X = % d and variable Y = % d", x, y)
 
The console message method accepts optional parameters to allow you to replace the variables in the message with values. For example, you can send a function report error to the console:
FunctionSenderrorconsole (errorcode) {window. Console. Error ("Error: % s occured", Errorcode );}

Execute scripts and commands on the console

At the bottom of the "console" Pane on the "console" tab or "script" tab, You can execute one or more lines of console commands or script statements. You can execute any valid script commands or expressions in the console.

For example, to view variable values, type a name in the console and pressEnter. To change the value of a variable in the script, enter the assigned value in the console. Press the up key to browse the commands previously executed.

You can use the "console" Pane on the "scripts" tab whether or not to start the debugging program. When the breakpoint is stopped, the command entered in this pane runs within the breakpoint scope. When the execution is not paused, the command runs in the global scope.

Use Cd () to execute commands across frameworks

By default, script statements and commands are executed in the context of top-level windows. If the frame is used, use the "CD ()" Console Command.

CD ()

CD (window)

You can change the default top-level window of a webpage to a frame based on the command line expression. If you call Cd () without parameters, the top-level window is returned.

Demonstrate the steps in the example provided here.

Run the following command on the top of the console View:

    • CD ()-print the current window.
    • CD (myframe)-change the expression calculation to an example frame whose ID is "myframe.
    • Counter-display the global variable "counter" in IFRAME.
    • Counter = 25-change the counter value to 25.
    • CD ()-change the expression calculation back to the default top-level window.
    • Counter-in this example, the counter is not a valid variable in the top-level window.

You can use the ID or frames [] set to change it to IFRAME. In this example, "document. Frames [0]" also plays a role.

Execute multi-line scripts

To run the multi-line script command, click the multi-line mode button or press CTRL + ALT + M. In the multiline window, TypeScriptAnd then click "run script. Unlike the single-line mode for executing a scriptEnterA line feed is added to the script window. You can right-click an input window that can be resized or shortcut menus (such as copy and paste) and Unicode functions to have other controls.

Filter messages and expand console objects

You can filter console messages from the console pane to display or hide messages of Certain types. To filter messages, right-click the "console" pane and hover the cursor over the "filter. A list of available filters is displayed, and the filters with check marks are active.

You can expand console objects to add new features. For example, you may want to customize a method to output formatted debugging messages to the console. To add the "console. debug" command, you can add the following code segment to JavaScript code:

 
Console. DEBUG =Function(Name, value) {console. Warn ("Debug:" + name + "=" +Value );}

This example uses two parameters and outputs them to the console pane in a minimal format. However, you can customize function parameters and behaviors at will. In this way, the console object can be used to add any number of new commands required.

Note:Because you use existing console commands in the new command, the filter is still applied. For example, the console. DEBUG command in the previous example uses console. Warn to output messages to the console pane. If "console warning" is cleared from the "filter" list, no output from console. Warn is displayed in the "console" pane.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.