Controlling Browser Properties with JavaScript - Location is Everything (
Page 6 of 8 )
If it's the browser URL you want to manipulate, you can't go wrong with the Location object, which exists under the Window object. This object has two important methods and three important properties - the latter are illustrated in the example below:
<script language="JavaScript">
window.alert("URL = " + location.href +
"nHost = " + location.host + "nProtocol = "
+ location.protocol)
</script>
The "href", "host" and "protocol" properties of the Location object return the current URL, host name and protocol being used respectively. Of these, you can use the "href" property to redirect the browser to a new URL, simply by giving it a new value. The following example shows you how:
<script language="JavaScript">
// send browser to new URL
window.location.href = "http://www.melonfire.com/";
</script>
An alternative way to accomplish the above is to redirect the browser to a new URL with the replace() method, as in the following example:
<script language="JavaScript">
// send browser to new URL
window.location.replace("http://localhost/aa");
</script>
You can also refresh the page by reloading it with the browser's current URL with the reload() method. This method comes in particularly handy if you need to refresh a page at a pre-defined interval. Consider the following example, which illustrates:
<script language="JavaScript">
setTimeout("location.reload()",600000)
</script>