3

For debugging purposes, I usually use something like console.log('line number #').

Not sure if it's the best way to handle it but I think it would be helpful if I can just print out the line number of the line where I'm putting the console.log() dynamically.

Let's say:

1    //do something
2    if(file){
3        console.log('Line 3');
4        $('#uploads').css({ 'height' : 'auto' });
5    } else { 
6       console.log(getLineNumber());   //just an example
7       $('#tags').val(temp);
8    }

In the above, if I happen to remove line 1 for instance, line 3 will be technically incorrect as the line number is decremented by 1 but the log will still show 3. But in line 6, suppose getLineNumber() returns the line number, then it will still make sense even after a line above has been removed.

So is there an easy way which acts like getLineNumber()?

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
ptamzz
  • 9,235
  • 31
  • 91
  • 147

1 Answers1

1

You can use onerror event handler for that.

See the last example on this page: http://www.tutorialspoint.com/javascript/javascript_error_handling.htm

Direct link to example: http://www.tutorialspoint.com/cgi-bin/practice.cgi?file=javascript_40

Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57
  • 1
    But there will be cases which are not exactly errors. Maybe I would just want to check if the script execution went through inside an `if, else` condition or a loop. – ptamzz Feb 15 '12 at 08:41
  • 1
    There is no clean solution but workarounds like this one `new Error().lineNumber` -> http://stackoverflow.com/questions/2343343/how-can-i-determine-the-current-line-number-in-javascript – Emir Akaydın Feb 15 '12 at 12:13