⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 display.php-n=15-9&f=15-log.js.htm

📁 javascript 5 随书源码 javascript
💻 HTM
字号:
<html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Keywords" name="Keywords"content="JavaScript, examples, JavaScript: The Definitive Guide"><title>Example 15-9 from JavaScript: The Definitive Guide, Fifth Edition</title><style>body {  font-family: arial, sans-serif}.lefttitle {  font: bold x-small arial, sans-serif;  margin: 5px;  letter-spacing: .15em;  text-transform:uppercase;}.leftcaption {  font: italic x-small sans-serif;  margin-bottom: 20px;}#banner {	font-family:sans-serif;	color:#333;	font-size:x-large;	font-weight:normal;		padding:15px;	padding-top: 3px;        border-bottom:4px double #666;}#banner h1 {	font-size: x-large;	margin: 0px;	padding: 0px;}#banner .description {	font-size:large;	margin-left: 2px;}pre {  border: solid black 1px;  background-color: #ddd;  padding: 10px;  margin: 0px;}	</style></script></head><body><div id="banner"><h1>davidflanagan.com/javascript5</h1><span class="description">Example 15-9 from<i>JavaScript: The Definitive Guide, Fifth Edition</i></span></div><div style="float: left; text-align: center; width:200px; margin-right:20px"><div class="lefttitle">Buy The Book</div><iframe src="../../rcm.amazon.com/e/cm-t=davidflanagancom&o=1&p=8&l=as1&asins=0596101996&fc1=000000&IS2=1&lt1=_blank&lc1=0000ff&bc1=000000&bg1=ffffff&f=ifr" tppabs="http://rcm.amazon.com/e/cm?t=davidflanagancom&o=1&p=8&l=as1&asins=0596101996&fc1=000000&IS2=1&lt1=_blank&lc1=0000ff&bc1=000000&bg1=ffffff&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe><div class="leftcaption">This example is posted here for the convenienceof my readers.</div><div class="lefttitle">Tip the Author</div><form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="image" src="../../www.paypal.com/en_US/i/btn/x-click-but21.gif" tppabs="https://www.paypal.com/en_US/i/btn/x-click-but21.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!"><img alt="" border="0" src="../../www.paypal.com/en_US/i/scr/pixel.gif" tppabs="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"><input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHXwYJKoZIhvcNAQcEoIIHUDCCB0wCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYADQkftkyvD87v5P15OJbQkb2IbGl9tDzTbZhLKhlUbLBQrkhCfnfuTM9jU8nFwjJPk0ydZnkIuM3mXoIlMrAWnTpmJPb2+JmRW4cdPmy7ET8SEayKGZVuh/z0HwP7XSWjYfN91QC8Dw0zvI575PV3Kggn/Hr0pbLO7W9TY4qX88TELMAkGBSsOAwIaBQAwgdwGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIxhIQ3LNTLmaAgbhxMeck3iCr8ij6ii9FtlNqTPr6xI86COz9DBGf1iPolPb9mEhyUUHOeRcZ0iZ4kP0hU+umGxnu8dF67WYwoD3Dpj4js/kzHA9/kO0GdPaonWkZBfR+i005EavkgNZ/atz7uFT2JOQ5kJEuRBXo0MmfVA1WoBus2fmgc3Hd9peXJ+eoUvEdmYV5EV6Rwn0xny1o3oY/jnDk8oh7fwxRYmVhWni9NZ2TyU27hR7j3TxijUbIGeGSzYTooIIDhzCCA4MwggLsoAMCAQICAQAwDQYJKoZIhvcN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N0v4gUkEDI8r2/rNk1m0GA8HKddvTjyGw/XqXa+LSTlDYkqI8OwR8GEYj4efEtcRpRYBxV8KxAW93YDWzFGvruKnnLbDAF6VR5w/cCMn5hzGCAZowggGWAgEBMIGUMIGOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxFDASBgNVBAoTC1BheVBhbCBJbmMuMRMwEQYDVQQLFApsaXZlX2NlcnRzMREwDwYDVQQDFAhsaXZlX2FwaTEcMBoGCSqGSIb3DQEJARYNcmVAcGF5cGFsLmNvbQIBADAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMDYwODA3MDYwODQzWjAjBgkqhkiG9w0BCQQxFgQULV3HGvs2maVapQx6JqKdwMYPb9IwDQYJKoZIhvcNAQEBBQAEgYBkwPTf1ZY3TQkhiYlHNTzcz6cl9v+HZPpuKG07vXkfKZjRbQ+kTH7AnumG1l/+aVUXevEa6IF7156JW5P8pHyNwKK59diT/2CfkCgo2NpK2UfJ88O+83gFLxM8NLjZCra0lHb68MDpNPk1HSxfb2CJN5wkJyWsrAwHYyzlnT5hFg==-----END PKCS7-----"></form><div class="leftcaption">Found a helpful example, but don't own the book?</div><div class="lefttitle">Advertising</div><script type="text/javascript"><!--google_ad_client = "pub-5162555666633441";google_alternate_color = "ffffff";google_ad_width = 160;google_ad_height = 600;google_ad_format = "160x600_as";google_ad_type = "text_image";google_ad_channel ="";//--></script><script type="text/javascript"  src="../../pagead2.googlesyndication.com/pagead/show_ads.js" tppabs="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div><div style="margin-left: 220px;"><div class="lefttitle" style="padding:0px; text-align: right"><a href="index.html" tppabs="http://www.davidflanagan.com/javascript5/index.html">Table of Examples</a></div><pre>/* * Log.js: Unobtrusive logging facility * * This module defines a single global symbol: a function named log(). * Log a message by calling this function with 2 or 3 arguments: *  *   category: the type of the message.  This is required so that messages *     of different types can be selectively enabled or disabled and so *     that they can be styled independently.  See below. * *   message: the text to be logged.  May be empty if an object is supplied * *   object: an object to be logged.  This argument is optional.  If passed, *     the properties of the object will be logged in the form of a table. *     Any property whose value is itself an object may be logged recursively. *  * Utility Functions: * *   The log.debug() and log.warn() functions are utilities that simply  *   call the log() function with hardcoded categories of "debug" and *   "warning".  It is trivial to define a utility that replaces the built-in *   alert() method with one that calls log(). *  * Enabling Logging * *   Log messages are *not* displayed by default.  You can enable the *   display of messages in a given category in one of two ways.  The *   first is to create a &lt;div&gt; or other container element with an id *   of "&lt;category&gt;_log".  For messages whose category is "debug", you might *   place the following in the containing document: *  *      &lt;div id="debug_log"&gt;&lt;/div&gt; * *   In this case, all messages of the specified category are appended *   to this container, which can be styled however you like. *  *   The second way to enable messages for a given category is to *   set an appropriate logging option.  To enable the category *   "debug", you'd set log.options.debugEnabled = true.  When you *   do this, a &lt;div class="log"&gt; is created for the logging messages. *   If you want to disable the display of log messages, even if a container *   with a suitable id exists, set another option: *   log.options.debugDisabled=true.  Set this option back to false to  *   reenable log messages of that category. * * Styling Log Messages *  *   In addition to styling the log container, you can use CSS to *   style the display of individual log messages.  Each log message *   is placed in a &lt;div&gt; tag, and given a CSS class of *   &lt;category&gt;_message.  Debugging messages would have a class "debug_message" *  * Log Options *  *   Logging behavior can be altered by setting properties of the log.options *   object, such as the options described earlier to enable or disable logging *   for given categories.  A few other options are available: * *     log.options.timestamp: If this property is true, each log message *       will have the date and time added to it. * *     log.options.maxRecursion: An integer that specifies the maximum number *       of nested tables to display when logging objects.  Set this to 0 if *       you never want a table within a table. * *     log.options.filter: A function that filters properties out when logging *       an object.  A filter function is passed the name and value of *       a property and returns true if the property should appear in the *       object table or false otherwise. */function log(category, message, object) {    // If this category is explicitly disabled, do nothing    if (log.options[category + "Disabled"]) return;    // Find the container    var id = category + "_log";    var c = document.getElementById(id);    // If there is no container, but logging in this category is enabled,    // create the container.    if (!c &amp;&amp; log.options[category + "Enabled"]) {        c = document.createElement("div");        c.id = id;        c.className = "log";        document.body.appendChild(c);    }    // If still no container, we ignore the message    if (!c) return;    // If timestamping is enabled, add the timestamp    if (log.options.timestamp)        message = new Date() + ": " + (message?message:"");    // Create a &lt;div&gt; element to hold the log entry    var entry = document.createElement("div");    entry.className = category + "_message";    if (message) {        // Add the message to it        entry.appendChild(document.createTextNode(message));    }    if (object &amp;&amp; typeof object == "object") {        entry.appendChild(log.makeTable(object, 0));    }    // Finally, add the entry to the logging container    c.appendChild(entry);}// Create a table to display the properties of the specified objectlog.makeTable = function(object, level) {    // If we've reached maximum recursion, return a Text node instead.    if (level &gt; log.options.maxRecursion)        return document.createTextNode(object.toString());        // Create the table we'll be returning    var table = document.createElement("table");    table.border = 1;        // Add a Name|Type|Value header to the table    var header = document.createElement("tr");    var headerName = document.createElement("th");    var headerType = document.createElement("th");    var headerValue = document.createElement("th");    headerName.appendChild(document.createTextNode("Name"));    headerType.appendChild(document.createTextNode("Type"));    headerValue.appendChild(document.createTextNode("Value"));    header.appendChild(headerName);    header.appendChild(headerType);    header.appendChild(headerValue);    table.appendChild(header);    // Get property names of the object and sort them alphabetically    var names = [];    for(var name in object) names.push(name);    names.sort();    // Now loop through those properties    for(var i = 0; i &lt; names.length; i++) {        var name, value, type;        name = names[i];        try {            value = object[name];            type = typeof value;        }        catch(e) { // This should not happen, but it can in Firefox            value = "&lt;unknown value&gt;";            type = "unknown";        };        // Skip this property if it is rejected by a filter        if (log.options.filter &amp;&amp; !log.options.filter(name, value)) continue;        // Never display function source code: it takes up too much room        if (type == "function") value = "{/*source code suppressed*/}";        // Create a table row to display property name, type and value        var row = document.createElement("tr");        row.vAlign = "top";        var rowName = document.createElement("td");        var rowType = document.createElement("td");        var rowValue = document.createElement("td");        rowName.appendChild(document.createTextNode(name));        rowType.appendChild(document.createTextNode(type));        // For objects, recurse to display them as tables        if (type == "object")             rowValue.appendChild(log.makeTable(value, level+1));        else             rowValue.appendChild(document.createTextNode(value));        // Add the cells to the row, and add the row to the table        row.appendChild(rowName);        row.appendChild(rowType);        row.appendChild(rowValue);        table.appendChild(row);    }        // Finally, return the table.    return table;}// Create an empty options objectlog.options = {};// Utility versions of the function with hardcoded categorieslog.debug = function(message, object) { log("debug", message, object); };log.warn = function(message, object) { log("warning", message, object); };// Uncomment the following line to convert alert() dialogs to log messages// function alert(msg) { log("alert", msg); }</pre><div class="lefttitle" style="padding:0px; text-align: right"><a href="index.html" tppabs="http://www.davidflanagan.com/javascript5/index.html">Table of Examples</a></div><p><script type="text/javascript">amazon_ad_tag="davidflanagancom"; amazon_ad_width="300"; amazon_ad_height="250"; amazon_ad_logo="hide";</script><script type="text/javascript" src="../../www.assoc-amazon.com/s/ads.js" tppabs="http://www.assoc-amazon.com/s/ads.js"></script></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -