|
Departments Info Home Page Articles Software GoodiesExplanations How do they do that? The Web in FocusResources Web Site Stuff Tech Books E-Books Tech Toys Web Hosting LinksWomen Opinion Tech Women Women's StudiesTech News Computer Security Databases Java Linux MP3 PC Software Robotics Site Owner Tech Latest Web Development XMLSpread the Word Newsletter
Recommend this Page
Site Info Legal Disclaimer
Privacy
Contact
Lighter Side Crazy for Life crazy for romance Crazy for Kitties Crazy for Dogs Crazy for CowsCopyright 2000-2001, hertechnology.com |
Your Third Program Last "Focus", we talked about using an if test to decide whether to print out "Hey Netscape user!" or "Hey IE user!" depending upon the type of browser the user was running. The second program compared the value of navigator.appName (which tells us the name of the browser) with the value "Netscape". If the two values were exactly the same, we printed out "Hey Netscape user!". If they were at all different -- even if the browser was name "netscape" or "Netscape Navigator", we would print out "Hey IE user!". So just in case the browser name isn't exactly "Netscape" we should look for the presence of the word "Netscape" any where in the browser name. We do that with a JavaScript built in function called indexOf. Functions A program is made up of statements that tell the computer to do something. These statements are rather like steps in a recipe. We can take a group of statements that perform a specific task and group them together and give the group a name. The group is called a function. A function can be compared to a recipe in this example. Let's say you are making a dish that has a dressing. You are working through the instructions of the main recipe when you see a statement like "Prepare the dressing, recipe follows". You look down and see a recipe called "Dressing" with all the steps for making the dressing. The Dressing recipe is like a function. It has a name, and it is a group of statements that perform specific task. To use a function in a program, we "call" it. That is analogous to the statement "prepare the dressing". The main recipe "calls" the dressing recipe. You work your way through the main recipe steps, then switch to making the dressing when the "call" takes place. When you are finished with the dressing, you go back to where you left off in the main recipe and continue. The same thing happens with a function in a program. The computer performs the statements in the program, and if it sees a function call, it goes and performs the statements in the funciton. When it is done with the function, it continues where it left off. Programming languages normally supply built in functions. This means the statements have already been written and named for the programmer. All that is needed is to call the function when you want its action performed. indexOf is one such function in JavaScript. There is something else you need to know about functions, and there really isn't an analogy with a recipe here. A programmer can pass information to a function. The function will use that information to help it perform its task. indexOf needs information to do its job, so we'll pass that information to it when we call it. Of course, there is a particular syntax we have to follow when passing information to a function so it does its job correctly. indexOf's particular task is to see if a value is contained in another value. For example, the word "Netscape" is contained in the value "Netscape Navigator". Another thing functions can do is return a piece of information when they are finished. indexOf will return a value of -1 if the value Netscape is not in the browser name. The last point to make about indexOf is that it is a particular kind of function called a method. All you need to know about a method is that it is attached to something. You don't just use it on its own. We can't just say "indexOf" by itself. We have to use indexOf with the something it is attached to. indexOf can be attached to different things, one of them is navigator.appName. That is good -- that means we can use indexOf to find out if "Netscape" is in navigator.appName. We have to use JavaScript's syntax for this, of course, so we say this (with all the .'s): navigator.appName.indexOf("Netscape") The parentheses after indexOf contain the information indexOf needs to do its job. In this case it is the value we want it to look for in the browser name. If navigator.appName is "Microsoft Internet Explorer", indexOf will return -1. If navigator.appName is "Netscape Navigator" it will return something other than -1 (in this case, a 0). |
|