My first JS component-cross-browser JS debugging tool

Source: Internet
Author: User
Recently, looking at the various components written by a company's JS Daniel, we have to start with the easy process. I read a JS debugging tool class and found the problem during the Demo writing process. As a result, I searched for some information on the Internet and wrote a JS debugging tool by myself.

Recently, looking at the various components written by a company's JS Daniel, we have to start with the easy process.

I read a JS debugging tool class and found the problem during the Demo writing process.

As a result, I searched for some information on the Internet and wrote a JS debugging tool by myself.

Actual needs

JS debugging is not convenient. alert is unfriendly and time-consuming.

I learned that the browser has a built-in Console object (not in JavaScript), but different browsers support different methods.

Therefore, it is necessary to encapsulate the Console method to adapt to different browsers.

Tool JS source code
/*** FansUnion-JS-Log1.0 ** QQ: 240370818 * Email: LeiWen@FansUnion.cn * Date: 2013.12.9 * Copyright 2013-2014, leiwen * // debug tool class var LogUtil = (function (win, doc) {var LogUtil = win. logUtil | {}; // you can use LogUtil by default. enable = true; // The following four methods, IE/Firefox/Chrome/Opera Support // console-info message LogUtil.info = function (msg) {LogUtil. doLog (msg, 'info') ;}; // console-error LogUtil. error = function (msg) {LogUtil. doLog (msg, 'error') ;}; // console-warn warns LogUtil. warn = function (msg) {LogUtil. doLog (msg, 'warn') ;}; // console-log, can be displayed (in Firefox, All, error-warning-message-debugging information is not displayed) logUtil. log = function (msg) {LogUtil. doLog (msg, 'log') ;}; // The method supported by one or more browsers may not be supported by some browsers. If not, no error is reported, no prompt/debug LogUtil. debug = function (msg) {LogUtil. doLog (msg, 'debug') ;}; // you can call a specific LogUtil method in a browser based on your needs. doLog = function (msg, level) {var useable = LogUtil. isUseable (level); // available to call if (useable) {win. console [level] (msg) ;}; // whether the console method is available, the methods supported by IE/Firefox/Chorome/Opera are different // log info warn error assert dir clear profile profileEnd on the IE console // log info warn error debug exception assert dir dirxml trace on the Firebug Console // group groupEnd groupCollapsed time timeEnd profile profileEnd count clear table policyfirebug firebug // Chrom console profiles memory debug error info log warn dir dirxml trace assert count markTimeline/profile profileEnd time timeEnd group groupCollapsed groupEnd // Opera console time timeEnd trace profile profileEnd debug log info warn error assert dir // dirxml group groupCollapsed groupEnd count table // determine whether debugging of a certain level is available (level = error, warn, info, debug, etc.) LogUtil. isUseable = function (level) {var useable = LogUtil. enable & win. console & win. console [level]; return useable;} return LogUtil;}) (window, document );

 

Test example

 

Related Article

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.