We have now discussed how to write code and how to check that the syntax of the code is correct. The final step is to run the code and have the computer produce the result that we want.
As with syntax checking, we need to use software to run the code that we have written.
In the case of HTML, there are many software programs that will run the code, but the most common type is a web browser, such as Internet Explorer or Firefox.
All that we need to do to run our HTML code is to open the file containing our code with a web browser. The file does not have to be on another computer on the internet. In most browsers, there is a File menu with an option to open an HTML file on the local computer. We can then see whether the code has produced the result that we want.
Web browsers tend to be very lenient with HTML syntax. If there is a syntax error in HTML code, most web browsers try to figure out what the code should do (rather than reporting an error). Unfortunately, this can lead to problems where two different web browsers will produce different results from exactly the same code.
Another problem arises because most web browsers do not completely implement the HTML standards. This means that some HTML code will not run correctly on some browsers.
The solution to these problems, for this book, has two parts: we will not use a browser to check our HTML syntax (we will use HTML Tidy instead; see Section 2.5), and we will use a single browser (Firefox2.5) to define what a piece of HTML code should do. Furthermore, because we will only be using a simple subset of the HTML language, the chance of encountering ambiguous behavior is small.
If we run HTML code and the result is what we want, we are finished. However, more often than not, the result is not what we want. The next section looks at how to resolve problems in our code.
When we have code that has correct syntax and runs, but does not behave correctly, we say that there is a bug in our code. The process of fixing our code so that it does what we want it to is called debugging the code.
It is often the case that debugging code takes much longer than writing the code in the first place, so it is an important skill to acquire.
The source of common bugs varies enormously with different computer languages, but there are some common steps we should take when fixing any sort of code:
Chapter 3 provides some basic information about common HTML elements and Section 3.3 provides some good starting points for detailed documentation about HTML.
We will discuss specific debugging tools for some languages as we meet them.
If code does not produce the right result, the code should be modified in small, disciplined, and deliberate stages to approach the right result.
Paul Murrell
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 New Zealand License.