0

I'm working with Remy Sharp's twitterlib: https://github.com/remy/twitterlib, and in the example file, he uses log(//stuff) to output to the screen. I had previously only encountered document.write() to fill this capacity, and only seen log() in conjunction with console logging. Here's the snippet (look at lines 4 and 6):

var count = 0, limit = 2;
twitterlib.timeline('rem', { limit: 5 }, function (tweets) {
  count++;
  log('<strong>Tweets requested in hit ' + count + '</strong>');
  for (var i = 0; i < tweets.length; i++) {
    log(this.ify.clean(tweets[i].text));
  }

  if (count < limit) {
    this.next();
  }
});

I can't find any information on log() as a standalone javascript method for outputting to the screen, so I'm wondering if it's a good idea to continue to use this, or whether I should write to the screen another way (e.g. document.write() or jQuery DOM manipulation) for more predictable results.

Any info?

Tom Keenoy
  • 3
  • 1
  • 2
  • Check it: http://stackoverflow.com/questions/4743730/what-is-console-log-and-how-do-i-use-it – Justin Helgerson Apr 19 '13 at 18:03
  • `log` by itself is not a native _JavaScript_ function. Perhaps you mean [`console.log`](https://developer.mozilla.org/en-US/docs/DOM/console.log) as suggested by others here. I'd always advise against using [`document.write`](https://developer.mozilla.org/en-US/docs/DOM/document.write) as it can cause race conditions and if `document.open` needs to be called internally by it, will empty your page. – Paul S. Apr 19 '13 at 18:08
  • Post below by huwiler answered the question: the call is not actually to a native js method, but to a defined function, which I totally missed when skimming the sample code. – Tom Keenoy Apr 19 '13 at 19:06

1 Answers1

0

If you look right above that code, you'll find log's function definition:

function log(s) {
    document.getElementById('debug').innerHTML += '<p>' + s + '</p>';
}

You see log in the wild a lot due to the fact that Paul Irish included it as a wrapper to console.log in html5bootstrap; but in these cases it prints to the console not to the window. Generally, you should avoid document.write and use asynchronous-friendly methods like above instead. See Douglas Crockford's explanation why here: http://javascript.crockford.com/script.html.

huwiler
  • 915
  • 9
  • 9