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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With