Many of the topics about windows and frames that we are going to discuss are part of the JavaScript object model, which is a hierarchy of objects related to an HTML document. The top-most object of the model is the Window object which represents a window or a frame in a window. This is mostly used in referencing a secondary window that you are using to display something you want to draw attention to. This object branches out into five new objects: Document, Frame, Location, History, and Navigator. In the Document object the two lines you are already familiar with, the write() and writeln() methods, refer to the Document object. Within the Document object are several objects that all create an HTML document. You might be familiar with the form object and the anchor object in HTML. If you do know some Java, then you are familiar with the applet object. Below is the document object model. There are more objects than are shown, but that would be too many to prove the point.
How does this work? If you had a form that had a drop-down box, the path would be window.document.forms.select.options. To refer to a JavaScript object, you must refer to all its ancestors. Consider you are using a form that needs to process data. In order to determine the index number of the currently selected option you are using, you would type something like this: var ModelNumber = document.PRODUCT.MODEL.selectedIndex; ...where document refers to the document you created the form on, PRODUCT is the form you created in the document, and MODEL is the object you where the selectedIndex can be found. You can omit window because what you are viewing this tutorial in is the window() object. There will be times you need to use the window object like when you are handling events because the event-handlers automatically assume you are using the document. The same holds true if you are using other JavaScript objects. Say you don't have the variable set and you want to display an alert box, you must use alert(document.PRODUCT.MODEL.selectedIndex);. For the <IMG> tag, the proper syntax would be document.myImage.src;. Another way to refer to the window object is with the self object. self refers to the current active window. The two codes below can be considered to be identical:
window.alert("text string"); Below are two tables that describe the properties and methods that are associated with the window object:
As you saw in the second table, you are able to create pop-ups by opening a new window using the window.open("URL","name",options); method. You can include all or none of the arguments in the open() method, but the less you add, the less your users will be able to do with the new window. This is good if the pop-up is meant to be informational, however, if you want the user to go somewhere or do something after the window pops up, you'll want to add the appropriate options. To show how this is done, click here to open Google in a new window. The table below shows all of the options you can use with the open() method.
One word of warning: you can only use a Window's object name to specify a target window. To control a new window, you need to assign the Window object with the open() method, such as var newWindow = open("http://www.newsite.com");. You can then set the focus or close a window by typing newWindow.focus(); or newWindow.close(); respectively. Finally, there are four methods that are part of the Window object and are very useful in many applications. The first two are setTimeout() method and the clearTimeout() method are used to set and clear the amount of time required for a function to perform. This is how they should look:
var myTimer = setTimeout("myCodeThatExecutes", numberOfMilliseconds); The other two methods are setInterval() method and the clearInterval() method, which are similar to the first two except they repeatedly execute the same code. This is how they should look:
var myTimer = setInterval("myCodeThatExecutes", numberOfMilliseconds); |
|
Question: But, I can do the same in Java...and Java is an object orientated language. So, aren't you saying that JavaScript is also an object orientated language?
Answer: Well, to most people, JavaScript will never be a true OOL. In the next lesson, we'll learn how to use Frames and other objects.
If you have a questions about any of the lessons, feel free to ask.