Using location
The web browser changes the URL of a window when a user clicks on a hyperlink. However sometimes it is necessary to perform this operation yourself. It is achievable by setting the "location" property of the window object to a new URL.
The following example is going to redirect the user to a new web page since (in the imaginable situation) the requested page is no longer available. In other words JavaScript is going to manually change the current URL of the window to a new URL. The operation is implemented after the web page has loaded.
Run Code | View Code
Process
Changing the URL of the current window - Setting the location property of the window object in the "redirect" function
Events
The loaded document - on load event connected to the body tag
Input
- The file set on the location property - "location_new_page.htm"
- The string set as normal html in the document - "The Page you are looking for is no longer here I will redirect you to the new page in 4 seconds"
Event Trigger
The onload event will wait for four seconds( using "setTimeout" method) then call the "redirect" function.
The Solution
Make Event trigger
Register the onload event on the body element
<body onLoad="">
Make the onload event call the "setTimeout" method of the window object
<body onLoad="
setTimeout()">
Make the "setTimeout" method call the "redirect" function
<body onLoad=" setTimeout('
redirect()') ">
Make the "setTimeout" method call the "redirect" function after 4 seconds
<body onLoad=" setTimeout('redirect()',
4000) ">
Make the Redirect function
Function analyses
When this function is called by the "setTimeout" method which is triggered by
the "onLoad" event of the document object it changes the URL of the windrow to
a new file.
Comments with code
function redirect(){
Refer to the window so it's location property can be accessed then change the value of the property to a new file ("location_new_page.htm").
As soon as the "location" property is set the web browser will immediately download that file (If it can find it) and load it into the browser.
window.location = "location_new_page.htm"
}
Important
- Usefulness
Loading a new file into the web browser is not only useful when redirecting a user to a new page but it can also send the user to the web page designed for a specific web browser. Not only that but you can even make a select tag with a list of places where the user can go, once one of them are selected a script could load the corresponding page into the window.
- Be careful
If you open a new window with the "open" method of the window object and load a file into that window that is on a different server you will get permission denied error. This error occurs because of web browser security. To avoid this error you should never for example open a new window and load yahoo.com into it.
Run Code | View Code
See also making windows
Using alert
|
Using the status bar
|
Changing the background color of the document
|
Changing the content of a document
|
Using prompt
|
Using confirm
|
Querying a text field's value
|
Checking the selected value of a select object
|
Checking if a checkbox is selected
|
Checking if a radio button is selected in a group
|
Making a rollover
|
Preloading images
|
Getting the time
|
Using setTimeout/setInterval
|
Making windows
|
Making cookies
JavaScript Tutorial Home