Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trouble printing line numbers and `alert`ing long string vars to debug JS script block

Tags:

javascript

I need to incorporate some JS in my php.html page, and I am having a difficult time trying to debug. I have been googling but can't seem to find how to print line numbers, like __LINE__ in php. Is there a way to achieve this seemingly useful feat easily enough?

Another problem I have is, I am trying to debug a script block that is not quite behaving, and I need to echo, er make that alert a var that is a very long string. The alert box unfortunately cuts itself off, presumably because of how long the var string is. There doesn't seem to be anyway to define parameters, such as height, width, etc. for the alert box, so I have tried using this hack:

function alertDebug(linesToDisable)
      {
        var newLinesToDisable = new String();
        for (var n = 0; n < linesToDisable.length; n++)
        {
          if (n % 100 == 0)
            newLinesToDisable += "\n";
          newLinesToDisable += aString[n];
        }
        alert( newLinesToDisable );
      }
      alertDebug( linesToDisable );

but the alert fails to fire altogether. Can anyone with L337 JS skills help with as well?

like image 787
fake rooted Avatar asked May 01 '17 14:05

fake rooted


1 Answers

About debugging/long logs: the JavaScript console object has a lot of useful methods for debugging. You can find them all over at MDN. The most common one being console.log(newLinesToDisable).

About line numbers: usually browsers also expose the line number where the error was thrown (a stack trace), but if you want to specifically log a line number, you could try

var line = new Error().lineNumber

If that doesn't work in whatever environment you're using, you can try:

var stack = new Error().stack

But then you have to dig through the whole stack trace.

like image 144
kano Avatar answered Nov 04 '22 09:11

kano