3

I searched a lot but could not get correct / updated answer (arguments.caller.callee does not exist it seems). I want to know the code line number where a particular function is called. I got this solution where function caller name can be retrieved .

But I am calling my function in JQuery UI droppable. For eg:

$("#id").droppable({

stop: function(e,u){

   main(); // I want this line number
}

});

Thats the reason I wanted to know the line number . Is there any way ?

Community
  • 1
  • 1
Amar Singh
  • 5,464
  • 2
  • 26
  • 55

2 Answers2

1

If you use console.error(), it will show the call stack and the line numbers (at least in Chrome, when you click the small black arrow):

function foo(){
    bar();
}

function bar(){
    console.error("hello");
}

foo();

Will show:

enter image description here

Check this fiddle (console.error doesn't work properly in the snippet): https://jsfiddle.net/fs1qqe77/

Gerardo Furtado
  • 100,839
  • 9
  • 121
  • 171
0

I have try with try catch throw

Note: snippet works inside a iframe so line wont be correctly.

var i = 2016;
i++;
var str = "Happy new year "+i;

try{
clog();throw new error("l"); 
}catch(e){
console.log("current line: "+e.stack.split(" at ")[1]);//current file and line
}


function clog(){
console.log(str);
}
Mamdouh Saeed
  • 2,302
  • 1
  • 9
  • 11