Referencing Windows
When working with single and multiple frames in your javascript application, you probably need to use additional ways to reference windows. Javascript provides four references to windows. Each of the references are implemented as properties of the window object.
Window and Self
User can refer to the current window as window or self. For example , the following two code lines are functionally the same:
window.defaultStatus ="Welcome to the Goat Farm Home Page"
self.defaultStatus="welcome to the Goat Farm Home page"
Because both window and self are synonyms to the current window, you might find it curious that both are included in the javascript language. As shown in the previous example , the rationale is simply flexibility; You can use window or self as you want.
However, as useful as window and self can be , it can easily become confusing to think about the logic behind it all. After all , an object's property that is used as an equivalent term for the object itself is rather unusual. Consequently , you might find it helpful to think of window or self as "reserved words" for the window object rather than its properties.
Because window and self are properties of the window object, you cannot use both window and self in the same context. For example , the following code does not work as desired:
window.self.document.write("<h1>Test.</h1>")
Finally , in multiframe environments, window and self always refer to the window in which the Javascript code is executed.
Parent
As you learn later in this chapter, frames are the same as window objects within a frameset.
Within this multi-frame setting, you need to distinguish between the various frames displayed in the browser. The parent property of a window object helps you do that by referencing its parent-the window containing the <FRAMESET> definition. For example , if you want to retrieve some information about the current window's parent , you use the following example in Listing.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>child window</title>
</head>
<body>
<script language="javascript">
<!--function getParentInfo()
{
myParentTitle=parent.document.title
alert("My daddy's name is "+myParentTitle)
}//-->
</script>
<form>
<input type="button" value="Get Info" onclick="getParentInfo()">
</form>
</body>
</html>
Output
Working with Status Bar Messages
The status bar of a browser can be an important means of communicating with the user. You can use two properties of the window object-defaultStatus and status- to control the text that is displayed. You generally have two ways to use the status bar . First , you can display a default message on the status bar. The user sees this message without performing any action. You can display a default message using the defaultStatus property. The defaultStatus property can be set at any time - either upon loading the window or while the window is already opened.
Second , you can display a temporary message that overrides the default text. In practice , this message usually appears when a user performs an event , such as moving a mouse over a jump. You can set this message using the status property.
The code behind this page shows three actions that you code to change the status bar message. First , a default message is set when the window opens using following code:
window.defaultStatus = "Welcome to the large URL page"
Second , When the user passes over the "Go" link, its onMouseOver event handler calls the following function:
function changeStatus()
{
window.status="Click me to go to the Home page."
}
Third, to change the text of the default status message, the user can select a different message from the select object. When the Change button's onClick event handler is triggered , it executes the following function:
function changeDefaultStatus()
{
window.defaultStatus=window.document.statusForm.messageList.
}
Frame Object
The frame object is essentially the same element as a window object, and you can deal with it in a similar manner. If you are working with a single window, the window object is the top-level object. If you are working within a frameset , the top-level window is considered the parent window, whereas its child windows are considered frame objects.
Location object
The loaction object encapsulates the URL of the current page. Its enabling you to
Set the location object to move to a new URL.
Extract specific elements of the URL and work with them. Without the loaction object, you would be forced to perform string manipulations on a URL string to get at the information you need.

No comments:
Post a Comment