Javascript Location Object
The location object in JavaScript helps in storing the information of current URL of the window object. It is a child object of the window object.
Location object Properties & Methods :
Property |
Description |
Example |
location.href |
Represents a string specifying the entire URL |
https://www.freetimelearning.com/java/java-applications.php |
location.hostname |
Represents the server name, subdomain, and domain name of a URL |
freetimelearning.com |
location.pathname |
Returns the path and filename of the current page |
java-applications.php |
location.protocol |
returns the web protocol used |
http: or https: |
location.assign |
Loads a new document in the browser |
|
location.reload |
Reloads the current document that is contained in the location.href property |
|
location.replace |
replaces the current document with the specified new one, you can not navigate back to the previous document using the browser's Back button |
|
Example Program :
<html>
<head>
<title>JavaScript Location Object</title>
<script type="text/javascript">
function gotoLoc()
{
window.location.href = window.document.loctn.Protocol.
options[window.document.loctn.Protocol.selectedIndex].
text + document.loctn.Hostname.value + document.loctn.
Pathname.value
}
</script>
</head>
<body>
<h3>Enter URL in following sections</h3>
<form name="loctn" method="post">
<pre>Protocol:
<select name="Protocol" size="1">
<option>http://</option>
<option>file://</option>
<option>javascript:</option>
<option>ftp://</option>
<option>mailto:</option>
</select>
</pre>
<pre>
Host Name:
<input type="text" size="20" maxlength="256" name="Hostname" value="freetimelearning.com">
</pre>
<pre>
Path Name:
<input type="text" size="20" maxlength="100" name="Pathname" value="/">
</pre>
<pre>
<input type="button" name="Go" value="Go" onClick="gotoLoc()">
</pre>
</form>
</body>
</html>