Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Created March 19, 2015 09:36
Show Gist options
  • Save mindplay-dk/7673ed5800e6993efda9 to your computer and use it in GitHub Desktop.
Save mindplay-dk/7673ed5800e6993efda9 to your computer and use it in GitHub Desktop.
jquery.console.js
/**
* jQuery wrapper/plugin for console functions in FF/IE/Chrome.
*
* These functions execute silently when no console is available, so
* you can safely leave diagnotics calls in place during development
* and beta-testing.
*
* Examples:
*
* $.log('Hello, World.',1,2,3);
* $.warn('foo');
* $.assert(1 == 2, 'false'); // prints "false"
* $.assert(1 == 1, 'true'); // does not print
*
* Example with chainable functions:
*
* $('#test')
* .log('testing',1,2,3)
* .warn('warning!')
* .css('color','green');
*
* The log() and warn() methods accept variable number of arguments,
* same as the console.log() and console.warn() methods in FF/IE/Chrome.
*/
(function($) {
$.each(['log','warn'], function(i,fn) {
$[fn] = function() {
if (!window.console) return;
var p = [], a = arguments;
for (var i=0; i<a.length; i++)
p.push(a[i]) && (i+1<a.length) && p.push(' ');
Function.prototype.bind.call(console[fn], console)
.apply(this, p);
};
$.fn[fn] = function() {
var p = [this], a = arguments;
for (var i=0; i<a.length; i++) p.push(a[i]);
$[fn].apply(this, p);
return this;
};
});
$.assert = function() {
window.console
&& Function.prototype.bind.call(console.assert, console)
.apply(console, arguments);
};
})(jQuery);
/**
* examples:
*/
$(document).ready(function() {
$.log('Hello, World.',1,2,3);
$.warn('foo');
$.assert(1 == 2, 'false'); // prints "false"
$.assert(1 == 1, 'true'); // does not print
$('#test')
.log('testing',1,2,3)
.warn('warning!')
.css('color','green');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment