Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

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

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

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