Skip to content

Instantly share code, notes, and snippets.

@vaidd4
Last active July 18, 2017 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaidd4/da1fce7645fc1b6653df40f05ab0b3e1 to your computer and use it in GitHub Desktop.
Save vaidd4/da1fce7645fc1b6653df40f05ab0b3e1 to your computer and use it in GitHub Desktop.
A document.log() implementation
/**
* Add a 'pre' tag, with a specified id, to the page and log the arguments passed to it
*
* WIP:
* - support array & object deep log
* - option to log in console (last parameter)
*/
function documentLog (...arg) {
if (!arg || !arg.length) return
if (!((typeof arg[0]) === 'string')) return
let res, i, len
if (!(res = document.getElementById(arg[0]))) {
(res = document.createElement('pre')).setAttribute('id', arg[0])
document.body.appendChild(res)
}
for (i = 1, len = arg.length - 1; i < len; i++) res.innerHTML += arg[i] + ' '
res.innerHTML += arg[i]
}
/**
* Usage :
* First parameter is the id of the 'pre' tag to be used for logging
* You can log any js expression
*/
documentLog('someid', 'hello', window, typeof document, [1, 2, 3])
/**
* You can use it multiple times,
* if it find the specified id it will log the arguments to it
* else it will create a new 'pre' tag
*
* [JS Standard](https://standardjs.com/demo.html?gist=da1fce7645fc1b6653df40f05ab0b3e1)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment