Skip to content

Instantly share code, notes, and snippets.

@ricardodsanchez
Created August 19, 2014 13:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardodsanchez/eaa40cc6b1b6e7ef9529 to your computer and use it in GitHub Desktop.
Save ricardodsanchez/eaa40cc6b1b6e7ef9529 to your computer and use it in GitHub Desktop.
This script lets you safely use console log in your web application. Some browsers will throw an error when you have console.log("something") if the developer tools/console window are not open. To avoid this JavaScript errors, use the following script which checks for the existence of the console before trying to use it.
window.log = function () { log.history = log.history || []; log.history.push(arguments); if (this.console) { console.log(Array.prototype.slice.call(arguments)) } };
@ricardodsanchez
Copy link
Author

What is it?
This script lets you safely use console log in your web application. Some browsers will throw an error when you have console.log("something") if the developer tools/console window are not open. To avoid this JavaScript errors, use the following script which checks for the existence of the console before trying to use it. See original post explaining the how and the whys: http://www.paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/

How to add to your project:
Create a new js file (i.e. console.js) and paste the script shown below. Then just add this js file to the project where you want to log information to the console.

How to use it:

log('inside coolFunc',this,arguments); log(someobject);
etc...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment