Beginning the Process of Writing Scripts
- Initial Decisions
- Starting Your Script
- Your First Script
In This Chapter
Starting Your Script
Your First Script
Now we are going to tackle the task of writing your first scripts, which is what excites us all. We are going to keep it simple for a specific purposeto build a solid foundation on good programming practices. We will cover each step of creating this simple example in detail, so that we do not have to cover the longer examples in as much detail later in the book.
To do this, we are going to approach your first script the right way. By the right way, we mean that we are going to plan the script, implement it, and test it properly. While doing this, we will also remind you of some of the things you learned in the first two chapters.
Before you even start writing your code, you do have to make some initial decisions. These decisions will help you better understand what you have to do and how to write your code. The following sections will step you through this process with the objective of helping you understand the questions you should be asking yourself that ultimately lead to the creation of successful code.
What Browsers Do You Want to Support?
In fact, the language had a tendency to be all over the map until the ECMAScript standard was released. Because ECMAScript defines only the core portion of the language, however, it still leaves a lot of gray areas in client-side and server-side implementations. Because of this, you are going to spend a substantial amount of time trying to write scripts that work in all browsers. Hence the question, "What browsers do you want to support?"
In deciding what browsers you want to support, there are several factors that can help. They are as follows:
Do you have server-side functionality to help you determine the browser before pages are returned to the browser?
Do unsupported browsers need to fail gracefully?
Help from the Server Side
True, it might mean that you need several versions of your scripts, but the amount of download time for the browser and the chance of error will be greatly reduced. You are no longer sending code to determine the requesting browser because the excess lines needed to perform "client sniffing" are no longer needed. Having this functionality also allows you to better handle known non-supporting browsers gracefully.
Listing 3.1 Using the <noscript> Tag
Inline or src It?
The final question you need to ask is, "How do you want to put it on the page?" The question is a determination of where you want to put all of your code. This can either be in external library files that have been included using the src attribute, or directly in the document itself.
As we mentioned before, by placing your code in an external file, you will be able to edit one file to affect scripts on all of your pages. Additionally, if your code contains the characters < or &, it will not properly validate if you are attempting to create valid XHTML documents. Although this is not a problem for most implementations today, the momentum XHTML has in the market, from a standards perspective, could easily make this a requirement in the near future.
We recommend that you put all of your functions in external source files, which solves these problems. We even recommend you split up functions into separate files according to their functionality. For instance, you might have one file called calculations.js that contains reusable calculations, but another file called formhandling.js that contains form handling functions.
What Are Your Objectives?
When defining your objectives, you fully determine your objectives and ultimately the end result of your program. For instance, you may want it to perform error checking on forms. With this goal in mind, you should work backward to determine what will accomplish this goal. Using the form example, you should ask yourself how many elements are going to be in the form? How many are radio buttons, text fields, selection options, and so on that you want to be able to process?
After you have nailed down this second level of objectives, you can start figuring out how to code your scripts. You will want to write code that handles the radio buttons, text fields, and selection options. You may be able to create a function that will cover all the functionality you need and that simply works from a parameter passed in, such as "radio" to signify the handling of a radio button. Remember that code reuse is a good thing and that you should apply any coding experience you have to reducing the number of lines of code your scripts need.