18

Is there any way to get the source line number in Javascript, like __LINE__ for C or PHP?

Greg
  • 316,276
  • 54
  • 369
  • 333
too much php
  • 88,666
  • 34
  • 128
  • 138

7 Answers7

10

There is a way, although more expensive: throw an exception, catch it immediately, and dig out the first entry from its stack trace. See example here on how to parse the trace. The same trick can also be used in plain Java (if the code is compiled with debugging information turned on).

Edit: Apparently not all browsers support this. The good news is (thanks for the comment, Christoph!) that some browsers export source file name and line number directly through the fileName and lineNumber properties of the error object.

David Hanak
  • 10,754
  • 3
  • 31
  • 39
  • 2
    browsers which add a stack trace to error objects might also expose a `lineNumber` property directly - no need for parsing there... – Christoph Jan 18 '09 at 10:49
6

The short answer is no.

The long answer is that depending on your browser you might be able to throw & catch an exception and pull out a stack trace.

I suspect you're using this for debugging (I hope so, anyway!) so your best bet would be to use Firebug. This will give you a console object; you can call console.trace() at any point to see what your programme is doing without breaking execution.

Greg
  • 316,276
  • 54
  • 369
  • 333
3

You can use this in vanilla JS:

function getLine(offset) {
    var stack = new Error().stack.split('\n'),
        line = stack[(offset || 1) + 1].split(':');
    return parseInt(line[line.length - 2], 10);
}

Object.defineProperty(window, '__LINE__', {
    get: function () {
        return getLine(2);
    }
});

You will now have access to the global variable __LINE__

kurdtpage
  • 3,142
  • 1
  • 24
  • 24
2

A __LINE__ in C is expanded by a preprocessor which literally replaces it with the line number of the current input. So, when you see

printf("Line Number: %d\r\n", __LINE__);

the compiler sees:

printf("Line Number: %d\r\n", 324);

In effect the number (324 in this case) is HARDCODED into the program. It is only this two-pass mechanism that makes this possible.

I do not know how PHP achieves this (is it preprocessed also?).

kjfletch
  • 5,394
  • 3
  • 32
  • 38
  • 4
    Yes, PHP is preprocessed. In fact, it's a recursive acronym: PHP -> PHP: Hypertext Preprocessor. The acronym originally was for *Personal Home Pages* though. – alex Feb 19 '10 at 05:53
2

You can try to run C preprocessor (f.e. cpp from GNU Compiler Collection) on your javascript files -- either dynamically with each request or statically, by making this operation be applied every time you change your javascript files. I think the javascript syntax is similar enough for this to work.

Then you'd have all the power of C preprocessor.

liori
  • 40,917
  • 13
  • 78
  • 105
1

I think preprocessing makes more sense, in that it adds no runtime overhead. An alternative to the C preprocessor is using perl, as in the 2 step procedure below:

1 – add “Line # 999 \n” to each line in the script that you want numbered, e.g.,

  alert ( "Line # 999 \n"+request.responseText);

2 – run the perl below:

cat my_js.js | perl -ane "{ s/Line # \d+ /Line # $. /; print $_;}" > C_my_js.js; mv  C_my_js.js  my_js.js
MIkee
  • 904
  • 8
  • 12
0

There is one workaround.

Usually the __ LINE __ combined with the __ FILE __ is used for marking a locations in code and the marking is done to find that location later.

However, it is possible to achieve the same effect by using Globally Unique Identifiers (GUID-s) in stead of the __ LINE __ and __ FILE __. Details of the solution reside in the COMMENTS.txt of a BSD-licensed toolset that automates the process.

Martin Vahi
  • 129
  • 1
  • 5