JavaScript Console Console usage detailed

Source: Internet
Author: User
Tags stack trace

Currently Firefox Plug-ins Firebug and Chrome for console support is relatively good, mainly Console.log (), Console.dir (), Console.debug (), Console.info (), Console.error () and other methods, can be read in a way to output variables and objects, which console.log () used in the console output, Console.dir () can be used to structure output HTML nodes, JS objects, etc., Console.debug (), Console.info (), Console.error () and Console.log

The code is as follows Copy Code
Console.log (object[, Object, ...])

Output a message at the console. If you have more than one parameter, the output separates the parameters with a space.
The first argument can be a string that contains the formatted placeholder output, for example:

The code is as follows Copy Code
? 1 Console.log ("The%s jumped over%d tall buildings", animal, count);

The example above can be replaced with code with the following unformatted placeholder output:

The code is as follows Copy Code
? 1 Console.log ("The", Animal, "jumped over", count, "tall buildings");

And, these two methods can be used in combination. If you use a formatted placeholder that provides more arguments than the number of placeholders, the extra arguments are appended to the string in a space-delimited manner, like this:

The code is as follows Copy Code
? 1 Console.log ("I am%s and I have:", MyName, Thing1, Thing2, thing3);

If the parameter is a JavaScript object, then the console output is not static text, but an interactive hyperlink, click the hyperlink to view the object's HTML, CSS, Script, Dom window, available formatted string%o instead of JavaScript objects.

The code is as follows Copy Code
? 1 Console.log ("Body tag is%o", document.body);

Format a list of strings:
format string type
%s string
%d,%i Integer (no support for digital type)
%f floating-point type (no support for digital type)
%o Linked objects
Console.debug (object[, Object, ...])
Prints a message in the console containing a hyperlink to the location of the code call. If you enter the command directly on the console, the hyperlink (as Console.log ()) does not appear.
Console.info (object[, Object, ...])
Output a message with an "info" icon in the console and a hyperlink to the location where the code is called.
Console.warn (object[, Object, ...])
Output a message with a warning icon in the console and a hyperlink to the location where the code is called.
Console.error (object[, Object, ...])
Output a message with an "error" icon in the console and a hyperlink to the location where the code is called.
Console.assert (expression[, Object, ...])
Tests whether an expression expression is true. If not true, a message is written at the console and an exception is thrown
Console.dir (object)
Outputting all the properties of an object in a list is a bit like the way you look at the Dom window.
Console.dirxml (node)
Output an XML source code for an HTML or XML element. Similar to what you see in the HTML window.
Console.trace ()
Prints an interactive stack trace of JavaScript execution in the point where it is called.
The stack trace details the functions on the stack, as as the "the" and "the" were passed as arguments to each function. You can click each function to take the Script tab, and click All argument value to inspect it in th e DOM or HTML tabs.
Console.group (object[, Object, ...])
Outputs a message and opens a nested block in which the contents of the block are indented. Call the Console.groupend () close block. This command can be nested in use.
Console.groupend ()
Closes the most recent block opened by Console.group.
Console.time (name)
Creates a timer named name, calling Console.timeend (name) to stop the timer and output the elapsed time (in milliseconds).
Console.timeend (name)
Stops the timer with the same name and outputs the elapsed time (in milliseconds).
Console.profile ([title])
Open the JavaScript performance test switch. The optional argument title is output at the beginning of the report when the performance test report is printed.
Console.profileend ()
Turn off the JavaScript performance test switch and output the report.
Console.count ([title])
Writes the number of times this line of code where count is called was executed. The optional argument title would print a message in addition to the number of the count.


But IE does not support console, so if the use of console in IE will be an error, there are two ways to solve this problem, the first method is to Firebug JS file introduced, so IE can use console. The second method is to manually create a console variable in IE, so that although not output debugging results, IE will not be an error, the code is as follows:

  code is as follows copy code

if (! Window.console) console = {};
Console.log = Console.log | | function () {};
Console.warn = Console.warn | | function () {};
Console.error = Console.error | | function () {};
Console.info = Console.info | | function () {};

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.