JavaScript Location Object
In this JavaScript tutorial, you will learn about JavaScript Location Object – assign(), reload(), replace(), JavaScript Window Object Methods viz alert(), blur(), setInterval() and clearInterval().
Methods of Location Object
assign():
The assign method of location object is used for loading a new document.
General syntax of assign method of location Object:
|
for example
|
The output of the above program is a button with message as
|
When this button is clicked, the site exforsys.com loads as a document.
reload():
This method of location object is used for reloading the current document. The currently displayed page in the window that owns the location object is reloaded. This means that the document that owns the Location object is to be reloaded.
General syntax of reload method of location Object:
|
for example
|
The output of the above program is a button with message as
|
When this button is clicked, the current document reloads.
replace():
As the name implies, the replace() method of location object replaces the current document with a new one. The current History entry is replaced with the URL specified in the replace method. NOTE: The programmer must know that after the replace method is called he or she cannot go back to the previous URL by using the back button.
General syntax of replace method of location Object:
|
The URL specified in argument is the new URL.
for example
|
The output of the above program is a button with message as
|
When this button is clicked, the current document is replaced with http://www.exforsys.com as document.
JavaScript Window Object Methods
In the section detailing the Introduction to JavaScript and DOM objects, this tutorial presented the list of methods used with Window object. These methods of Window objects are repeated below for referral. The syntax and usage of the methods of Window Objects with explanation in detail about each will be described in the next few sections of this tutorial.
The methods of Window Objects are:
- alert()
- blur()
- setInterval()
- clearInterval()
- setTimeout()
- clearTimeout()
- close()
- confirm()
- createPopup()
- focus()
- moveBy()
- moveTo()
- open()
- print()
- prompt()
- resizeBy()
- resizeTo()
- scrollBy()
- scrollTo()
alert():
By using the alert() method of window object, users can display an alert box with a message displayed in it as specified by the user.
The string passed to the alert method in argument displays an alert dialog box along with an Ok button inside it. NOTE: The alert() method is used to convey a message where the decision from the user is not required.
General Syntax of the alert() method of window object:
|
An example to understand the usage of alert() method of window object in brief:
|
The output of the above example is a button with the message:
|
When this button is clicked, the alert box is displayed with the message:
|
and an ok button displays in the alert box.
The string passed to the alert() method mentioned in the function exforsys() is Welcome!!! This is displayed in the alert box with the ok button.
If a user wants to display messages in alert box by having a line break in the next line then, this is achieved by placing a ‘\n’ between messages and concatenating the messages using the symbol +.
An example to understand the above concept:
|
The output of the above example is a button with the message:
|
When this button is clicked, the alert box is displayed with the message:
|
And an ok button is displayed in this alert box.
In the above example, the message Have a Good Day!!! is placed after ‘\n‘ and thus is displayed in the next line of the previous Welcome message.
blur():
The blur() method of a Window object removes the focus from the current window. The blur() method of a window object is used to take the focus away from the current window.
General Syntax of the blur() method of window object:
|
An example to understand the usage of blur() method of window object:
|
In the above example, the focus is taken away from the current window (the exforsys window). The window named exforsys is opened using the window.open. The focus from the current window (exforsys) is removed using the exforsys.blur() statement.
setInterval():
The setInterval() method of a window object takes two arguments. The first argument is a function or an expression and the second argument is milliseconds. The setInterval() method of a window object is used to call a function or evaluate an expression at specified intervals (in this example, milliseconds). The calling of function or expression is continued until the window is closed or until the clearInterval() method is called. The setInterval() method returns an ID value which is used as a parameter for clearInterval method.
General Syntax of the setInterval() method of window object:
|
The expression/function mentioned is evaluated at specified intervals (milliseconds).
clearInterval():
The setInterval() method of a window object is used to call a function or evaluate an expression at specified intervals. The interval set by the setInterval() method of a window object cancels with the clearInterval() method of a window object.
General Syntax of the clearInterval() method of window object:
|
Here, the intervalID mentioned in argument of clearInterval() method represents the ID value returned by the setInterval() method.