Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [147]

By Root 1491 0
program:

This code looks innocent enough, but if you run it, you’ll be mystified. It doesn’t crash, but it also doesn’t seem to do anything. If you follow the code step by step, you eventually see why. I initialize i to 1, and then repeat as long as i is greater than 10. See the problem? i is less than 10 right now, so the condition starts out false, and the loop never executes! I probably meant for the condition to be (i < 10). It’s a sloppy mistake, but exactly the kind of bone-headed error I make all the time.

I’m not showing you a screenshot of this program, because nothing happens. Likewise, I don’t show you a screenshot of the one in the next section because it doesn’t do anything useful either.


Managing the obsessive loop

The other kind of bad-natured loop is the opposite of the reluctant loop I de-scribe in the preceding section. This one starts just fine, but never goes away!

The following code illustrates an endless loop:

If you decide to run endless.html, be aware that it won’t work properly. What’s worse, the only way to stop it will be to kill your browser through the task manager. In the upcoming section “Catching Logic Errors,” I show you how to run such code in a safe environment so that you can figure out what’s wrong with it.

This code is just one example of the dreaded endless loop. Such a loop usually has perfectly valid syntax, but some logic error prevents it from running properly. The logic error is usually one of the following:

♦ The variable wasn’t initialized properly. The initial value of the sentry is preventing the loop from beginning correctly.

♦ The condition is checking for something that can’t happen. Either the condition has a mistake in it, or something else is preventing it from triggering.

♦ The sentry hasn’t been updated inside the loop. If you simply forget to modify the sentry variable, you get an endless loop. If you modify the variable after the loop has completed, you get an endless loop. If you ask for input in the wrong format, you may also get a difficult-to-diagnose endless loop.

Debugging Your Code

If you’ve written JavaScript code, you’ve encountered errors. It’s part of a programmer’s life. Loops are especially troublesome because they can cause problems even when the syntax is perfect. Fortunately, you can use some great tricks to help track down pesky bugs.


Letting Aptana help

If you’re writing your code with Aptana, you already have some great help available. Aptana gives you the same syntax-highlighting and code-completion features as you had when writing XHTML and CSS.

Also, Aptana can often spot JavaScript errors on the fly. Figure 3-5 shows a program with a deliberate error.

Aptana notifies you of errors in your code with a few mechanisms:

♦ The suspect code has a red squiggle underneath. Similar to a word processing spell checker.

♦ A red circle indicates the troublesome line. You can scan the margin to quickly see where the errors are.

♦ The validation pane summarizes all errors. You can see the errors and the line number for each. Double-click an error to enter that spot in the code. If the validation window is not visible, you can enable it by selecting Window⇒Show View⇒Validation from the menu system.

♦ You can hover on an error to get more help. Hover the mouse on an error to get a summary of the error.

Aptana can catch some errors, but it’s most useful at preventing errors with the automatic indentation and code assist features.


Debugging JavaScript on Internet Explorer

Internet Explorer has unpredictable

Return Main Page Previous Page Next Page

®Online Book Reader