Debugging in Javascript - Are We There Yet?
(Page 4 of 5 )
Okay now, let's get rid of the comment characters and the alert altogether. What? Still not working? There is one more problem. We have a string to which we are setting our text field value that uses a mix of single and double quotes. This is bad.
<script language="Javascript">
//The Script
function doIt(fName)
{
var fObj = document.getElementById(fName);
if(fObj.value == "")
{
fObj.value = 'You're really pushing my buttons!";
}
}
Change the single quote to a double one and then try again. It should work just fine. Let's hope that you won't have that many errors in your script, but by walking through it we got to the bottom of errors that otherwise might have taken a long time to find.
Some browsers (Firefox on OS X for instance) do not allow you to select and copy text from an alert box. This can be useful if a script you are writing generates HTML using DOM methods. This markup will not be visible using the "view source" browser feature. If you display the innerHTML property of a tag in an alert dialog, select it, copy it and paste it into a blank document, you can work with it to fix any problems or try different styles without running the script that generated it each time. If you can't select the text in the alert dialog, then you need another solution.
Next: Make a Log Window >>
More JavaScript Articles
More By Chris Root