JavaScript Frame Object
In this JavaScript tutorial, you will learn about frame object its properties and methods in detail with example. frame object, properties of frame object, frames, self, name, length, parent, methods of frame object, blur(), focus(), setInterval(), clearInterval(), setTimeout(expression, milliseconds), clearTimeout() and events associated with frame object.
Frame Object:
The frame object is a browser object of JavaScript used for accessing HTML frames. The user can use frames array to access all frames within a window. Using the indexing concept, users can access the frames array.
NOTE:
-
The frames array index always starts with zero and not 1.
-
The frame object is actually a child of the window object. These objects are created automatically by the browser and help users to control loading and accessing of frames.
-
The properties and methods of frame object are similar to that of Window object in JavaScript.
-
The frame object does not support close() method that is supported by window object.
-
Using the <FRAMESET> document creates frame objects and each frame created is thus a property of window object.
Properties of frame object:
- frames
- name
- length
- parent
- self
frames:
The frames property of frame object denotes a collection or array of frames in a window and also in a frame set.
self:
As the name implies, the self property of frames object denotes the current frame. Using self property, the user can access properties of the current frame window.
name:
The name property of frame object denotes the name of the frame. The method of denoting the name attribute is performed by using the name attribute of the <frame> tag.
For example it can be written as:
|
The above statement would store the name of the third window frame (as the frames array start with index 0) in a frameset document in the variable exforsys.
length:
The frames array has all the frames present within a window and the length property of the frame object denotes the length of the frames array or gives the number of frames present in a window or a frames array.
parent:
As the name implies, the parent property of frames object denotes the parent frame of the current frame.
Methods of frame object:
- blur()
- focus()
- setInterval()
- clearInterval()
- setTimeout(expression, milliseconds)
- clearTimeout(timeout)
blur():
blur() method of frame object removes focus from the object.
focus():
focus() method of frame object gives focus to the object.
setInterval():
setInterval() method of frame object is used to call a function of JavaScript or to evaluate an expression after the time interval specified in arguments has expired. The time interval in arguments is always specified in milliseconds.
For example:
|
In the above statement, the function test() executes after 2000 milliseconds (2 seconds), specified in the argument.
clearInterval():
clearInterval method of frame object is used to cancel the corresponding defined setInterval method. This is written by referencing the setInterval method using its ID or variable.
General syntax for the method clearInterval() is as below:
|
setTimeout(expression, milliseconds):
setTimeout method of frame object can be used to execute any function, or access any method or property after a specified time interval given to this method as argument.
General syntax for the method setTimeout() is as below:
|
For example:
|
The time is always specified in milliseconds and in the above statement, the function test() is called after the specified time of 3000 milliseconds (3 seconds). This is stored in variable named exforsys.
There is confusion about the similarity between setTimeout() method and setInterval() method. The main difference between the two methods is the setInterval method will repeatedly call the referenced function or evaluate the expression until the user leaves the document. In the setTimeout method, the call executes only once after the specified time interval given as argument.
clearTimeout():
clearTimeout method of frame object is used to clear a specified setTimeout method. This is written by referencing the setTimeout method using its ID or variable.
General syntax for the method clearTimeout is as below:
|
For example
|
The above statement clears the setTimeout associated wit the ID named as exforsys, created in the earlier example.
Events associated with frame object:
Though the frame object and frames array have no event handlers associated directly with them, the following event handlers are used to access and control frame objects and frames array:
- onBlur
- onFocus
- OnLoad
- OnUnLoad