Welcome to Australian PC User Magazine Offline CD-ROM
Software - What's on our latest cover CD. What's New - Like . . "What's new?" Net Guide - Our comprehensive guide to using the Net Web Workshop - Tutorials for Web developers Net Sites - Check out the best on the Web About Us - Find out about PC User Magazine and PC User Net nineMSN
clear.gif (118 bytes)
Web Workshop Contents

Home
Search
Help!

Framing Web Site menus - Using JavaScript with Frames

Help! Why won't it work?

When you find a piece of JavaScript code that you want to use in your own program, you'll most likely want to adapt it. This is where things can get a little tricky. Here are some tips for adapting your code to make the task easier and some answers to the very common question: 'What do I do, my code won't work?'

1. Copy and get the original code running. If you can't get the code that you're trying to adapt to run the way it was first written, there is little point in spending time adapting it! First get the code to run, then start the work of customising it for yourself.

2. Make haste slowly. Make your changes little by little and test each one as you go. This way you limit the number of statements that are likely to be causing you problems and make them easier to find. Until you get each change working, don't progress to the next one.

3. Form vs function. It's a temptation to spend time getting things looking pretty rather than getting them to work. The most wonderful looking clock is worth nothing if the JavaScript driving it doesn't work and the lousiest looking clock that works is at least functional! Add the pretty bits after you've got it working.

4. Use versions. Before starting each step of adapting or developing your code, save the last working original and copy its contents to a new file and work on this new copy. If you make a complete mess of your work you can go back to the previous copy and start working again from there.

5. Comment your work. We know it's much more fun to write code than it is to write comments -- we're programmers, too! But comments are invaluable when you're debugging or altering code. Save yourself hours of struggling to unscramble variable names and logic by writing the explanations for everything in comments in the code itself -- not on sticky yellow paper that will get lost!

6. Add your own checks. Most programming languages give you detailed debugging tools that allow you to insert break points, trace the values in variables and step through code one line at a time. If you're programming in JavaScript, you need to create these yourself. Add an alert box to a segment of code to indicate if your code is executing at that point or program it to tell you the value of a variable when you want to check it. Temporarily remove alert boxes by prefixing them with // to turn them into comments and remove them in the final version of your page.

7. Check and check it again. JavaScript is case-sensitive and many problems in your code can be traced to simply mistyping variable and function names -- in other words, mixing up upper- and lower-case letters. It's often difficult to trace these errors on the screen, so try printing your code and checking it from the hard copy.

8. Retype an offending statement. If you're sure a statement is correct and it still causes an error when executed, try removing the line and retyping it. It might not make a lot of sense at the time but as often as not the offending statement is fixed by this simple approach.

9. Change browser. If your code won't work and you can't find the problem, try running it in another browser. There are enough differences in the way each browser supports JavaScript that some time you're likely to come up against a line of code that won't work in one browser but will in another. Trying your code in another browser will help you identify these problems.

10. Look outside the line. A syntax error will stop your code from running and you'll see a JavaScript alert appear telling you there's a problem and the line number of the problem. Look at this line first -- it's most likely the problem is here. But, don't assume that the problem has to be in this line -- it doesn't. If you can't find the problem in the stated line, check the preceding lines as the problem might be there.

11. Beware of 'helpful' Tools. Unfortunately, tools such as HTML editing programs rather than 'helping' you can, instead, sabotage your JavaScript code. If you open a page containing JavaScript code in a program such as FrontPage 97 and make changes to it, you'll find it has a tendency to move the JavaScript code. If the code is moved from the <HEAD>...</HEAD> sections, it may no longer work in your browser. Avoid FrontPage and any other editing tool from messing with your code by opening your file in a text editor rather than using a Web editing tool. If you're using FrontPage 97, select Notepad as your editor.

corner.gif (190 bytes)

clear.gif (118 bytes)

toppage.gif (1757 bytes)copyrite.gif (1355 bytes)