JavaScript
You can include JavaScript code in your HTML document to redirect people
to pages that have moved or different pages based on browser type.
To redirect users to a new page, add the following code to the <HEAD> section
in your HTML document:
<HEAD>
<SCRIPT language="JavaScript">
<!--
window.location="URL";
//-->
</SCRIPT>
</HEAD>
where
| <SCRIPT language="JavaScript"> |
identifies the following code as JavaScript,
put the closing </SCRIPT> tag before the
closing </HEAD> tag. |
| <!-- |
code is enclosed in the comment tag so that it
will be
ignored by older browsers and browsers with JavaScript
disabled, put the closing //--> tag before the closing
</SCRIPT> tag. |
| window.location |
window is the object and location is its property;
sets the
URL of the current window to the URL you specified. |
| URL |
is the URL that should be loaded when this page
is
accessed. |
For example,
<HEAD>
<SCRIPT language="JavaScript">
<!--
window.location="http://www.utexas.edu/its/";
//-->
</SCRIPT>
</HEAD>
tells the browser to load the page, http://www.utexas.edu/its/, automatically.
The example below detects browser type and redirects viewers to different
pages based on their browser:
<HEAD>
<SCRIPT language="JavaScript">
<!--
if (navigator.appName=="Netscape")
{
window.location="netscape_index.html";
}
else if (navigator.appName=="Microsoft Internet Explorer")
{
window.location="ie_index.html";
}
else
{
window.location="other_index.html";
}
//-->
</SCRIPT>
</HEAD>
where
| navigator.appName |
navigator is the object and appName is its property;
appName is the name of the browser, for example, Microsoft Internet
Explorer. |
Advantages
- Control over the redirection without contacting the system administrator.
- Correct URL address appears in the browser's location box.
- New location can be on a different server.
Disadvantages
- Users can turn off JavaScript capabilities in their browser.
- Browser back button doesn't work.
More Information
|