论坛首页 Web前端技术论坛

JQuery读书笔记--Log函数

浏览 2364 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-01-20  
/**
 * @author erich
 */
(function($){
    var PREFIX_DEBUG = "DEBUG";
    var PREFIX_ERROR = "ERROR";

	
    $.debug = function(type,msg){
        chooseLogger(type, msg, PREFIX_DEBUG);
    };
    $.error = function(type,msg){
        chooseLogger(type, msg, PREFIX_ERROR);
    };
    
    var chooseLogger = function(type, msg, lever){
        switch (type) {
            case 'popup':
                //do popup logger
                popupLogger(msg, lever);
                break;
            case 'document':
                writeLogger(msg, lever);
                break;
            case 'console':
                consoleLogger(msg, lever);
                break;
            case 'alert':
                alertLogger(msg, lever);
                break;
        }
        
    };
    // loggers
    var popupLogger = function(pMessage, pLevel){
    
        var winName = "sb_log";
        var winAtt = "width=420,height=320,scrollbars=1,status=0,toolbars=0,resizable=1";
        var errorAlert = "The sb log window was blocked";
        var logTableId = "logTable";
        var logTableElement = "<table width='100%' id='" +
        logTableId +
        "'><tr><th align='left'>Time</th><th width='100%' colspan='2' align='left'>Message</th></tr></table>";
        
        // check if the window is already open
        if (!this._window || !this._window.document) {
        
            // open the window
            this._window = window.open("", winName, winAtt);
            if (!this._window) {
                alert(errorAlert);
                return;
            }
            if (!this._window.document.getElementById(logTableId)) {
                this._window.document.writeln(logTableElement);
                this._window.document.close();
            }
        };
        
        // add log message to window
        var tbl = this._window.document.getElementById(logTableId);
        var row = tbl.insertRow(-1);
        
        var cell_1 = row.insertCell(-1);
        var cell_2 = row.insertCell(-1);
        var cell_3 = row.insertCell(-1);
        cell_1.style.fontSize = "12px";
        cell_1.style.fontWeight = "bold";
        cell_1.style.paddingRight = "6px";
        cell_2.style.fontSize = "12px";
        cell_3.style.fontSize = "12px";
        cell_3.style.whiteSpace = "nowrap";
        cell_3.style.width = "100%";
        
        if (tbl.rows.length % 2 == 0) {
            row.style.backgroundColor = "#eeeeee";
        }
        
        var date = new Date();
        var hour = date.getHours();
        if (hour < 10) {
            hour = "0" + hour;
        }
        var minute = date.getMinutes();
        if (minute < 10) {
            minute = "0" + minute;
        }
        var second = date.getSeconds();
        if (second < 10) {
            second = "0" + second;
        }
        var date = (date.getMonth() + 1) + "/" + date.getDate() + "/" +
        date.getFullYear() +
        "&nbsp;-&nbsp;" +
        hour +
        ":" +
        minute +
        ":" +
        second;
        
        cell_1.innerHTML = date
        cell_2.innerHTML = pLevel;
        cell_3.innerHTML = pMessage;
    };
    var writeLogger = function(pMessage, pLevel){
        document.writeln(pLevel + "&nbsp;-&nbsp;" + pMessage + "<br/>");
    };
    
    var consoleLogger = function(pMessage, pLevel){
        if (jQuery.browser.mozilla) {
            console.log(pLevel + " - " + pMessage);
        }
    };
    
    var alertLogger = function(pMessage, pLevel){
        alert(pLevel + " - " + pMessage);
    };
})(jQuery);

论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics