![]() |
You've mastered HTML. Now move on to JavaScript and take control
of your user's browser.
As you develop a site on the Internet you need to remember that your Web page can be accessed by users all over the world using all kinds of browsers. Some of the browsers may not be compatible with Java-Script. Despite this caveat, the vast majority of users will access your site using some recent flavor of Microsoft Internet Explorer or Netscape Navigator. Although there are some major differences between Navigator and IE (with minor differences between versions of each) the two most recent versions of each browser support most basic JavaScript functionality. Let's start experimenting with JavaScript. Open Notepad and create a simple HTML test form with two text fields using this code:
One thing to rememberJavaScript is case-sensitive. If your code looks correct but does not work, check to make sure you have the right case for each letter. In the example above, Fname or fname will not work! When the browser loads an HTML page, a window object is created. The window object, just like other objects you work with, has a set of properties, methods, and events (see Table 1). Once created, you can use JavaScript code to set or read the properties, call the methods, and respond to the window object's events. For example, you can add an event handler for the onLoad event of the window object which is called when the browser finishes loading the window. To see how this works, replace the second line of code in your test file with these lines of code:
The last line sets the event handler for the onLoad event to the function AppDisplay. The AppDisplay function uses the alert statement to display a message box showing three properties from the navigator object. Notice the use of the \n escape sequence to place each of the properties on a new line. You can use these properties to identify the browser being used, its version, and the underlying operating system. For example, you can use this function to check which operating system a visitor uses. If you call this function in the onLoad event of the window, it will detect which operating system is running. The CheckOS function will redirect the browser to another URL if the user is using Windows 95, by using the indexOf method of the AppVersion property to see if it contains "95." It then sets the window object's location property to the new URL:
Pulldown menus
Note that the onChange event is handled by the NavTo function, and the Select control is passed to that function as an argument. Go back to the top of your file and add code for the NavTo function, that checks the selectedIndex property of the Select control to make sure it is not zero. Then set the window's location property to the selected option. Because the window object is the default object, it is not necessary to use the window.location syntax:
The window object contains several other objects in the browser's object model hierarchy. One of the most commonly used objects is the document object. The document object has several interesting properties. For example, the domain property specifies the domain name of the server that served the document, and the referrer property is the URL of the calling document. But it is the write method of the document object that gets the most use. For example, rather than displaying the name and e-mail address from your test form in a message box, you could use the open method of the window object to create a new window. Then, use the write method of the new window's document object to write in some HTML. First, change the Action property of your test form to call a ShowFields function and pass the form as an argument:
Then, add this procedure to open a new window and save a pointer to that window, in the NewWin variable. Notice that the third argument of the open method allows you to create a small, fixed-size window with no status bar or scroll bars. Then use the write method of the document object within the new window to write the HTML that will be displayed:
Now that you know enough of the browser's object model to open a new window and write some HTML, you can easily create a tool to help you learn the rest of the object model. JavaScript's standard for...loop statement is similar to the one in C. A block of statements is executed in the loop while a counter is increment to a limit:
But there is also a for...in statement similar to the for...each statement in Visual Basic. This statement iterates a specified variable over all the properties of an object. For each distinct property, JavaScript executes the specified statements. You can use this statement to iterate all the properties of an object. So why not create a routine that lets you select an object, and create a new window with a table showing all of that object's properties and values? Add this code to your test form to create another select control with values for the window, navigator, document, and form objects. The onChange event will call the SelObject procedure and pass the select control:
If you run this code for the form object (see Figure 3) you will see several interesting properties, including the inner HTML property that shows the actual HTML of the form. Now you have your own JavaScript object browser; one that will let you quickly learn the properties of the browser's object. Add some hyperlinks to your test HTML page, then look at the links collection of the document object. You can download the complete HTML page with all of the JavaScript object procedures from the registered area of DevX.
|