JavaScript Location Object Properties

JavaScript Location Object Properties

In this JavaScript Tutorial you will learn about Properties of Location Object – hash, host, hostname, href, pathname, port, protocol and search property

The Location object contains information about the current URL. This representation is a string that denotes the different portions of the URL as below:


<protocol>//<host>[:<port>]/<pathname>[<hash>][<search>]

The location object has a number of properties and methods in it. The usage, syntax and explanation with examples of each of the properties and methods of the Location object are explained in this section and the following sections.

Properties of Location Object:

hash:

The hash property of location object starts with the symbol hash denoted as #, specifying an anchor name in an URL. The hash property is also used for returning the URL.

General syntax of hash property of location Object:


location.hash = anchorname

for example


<
html>
   <
body>
      <script type="text/javascript">
          
document.write(location.hash);     

      </script>
   </body>
</
html>

Suppose the current URL is: http://xforsys.com/example.htm#test5

Then the output of the above program is


#test5

The location.hash in document.write takes the symbol starting from hash in the current URL and displays it giving the output as #test5

host:

The property of location objects are used for setting or returning the hostname and port number of the current URL.

General syntax of host property of location Object:


location.host

for example

The location.host in document.write takes the hostname in the current URL and displays it with the output exforsys.com


<
html>
   <
body>
      <script type="text/javascript">
          
document.write(location.host);     

      </script>
   </body>
</
html>

Suppose the current URL is: http://xforsys.com/example.htm#test5

Then the output of the above program is


xforsys.com

The location.host in document.write takes the hostname in the current URL and displays it with the output exforsys.com

{mospagebreak title=Properties: hostname, href and pathname}

hostname:

This has a similar function as hot property which was previously discussed. The only difference between hostname property of location object and hot property is the hostname property of location object is used for setting or returning the hostname only of the current URL.

General syntax of hostname property of location Object:


location.hostname

for example


<
html>
   <
body>
      <script type="text/javascript">
          
document.write(location.hostname);     

      </script>
   </body>
</
html>

Suppose the current URL is: http://xforsys.com/example.htm#test5

Then the output of the above program is


xforsys.com

The location.hostname in document.write takes the hostname in the current URL and displays the output exforsys.com

href:

If a programmer wants to set or return the entire URL, then the href property of location object can be used.

General syntax of href property of location Object:


location.href=URL

An example


<
html>
   <
body>
      <script type="text/javascript">
          
document.write(location.href);     

      </script>
   </body>
</
html>

Suppose the current URL is: http://xforsys.com/example.htm#test5

Then the output of the above program is


http://xforsys.com/example.htm#test5

The location.href in document.write takes the entire URL in the current URL and displays the output as displayed above.

pathname

As the name of this property suggests, the pathname property of location object is used for setting or returning the path of the current URL. This pathname specifies the path for accessing the particular URL or resource.

General syntax of pathname property of location Object:


location.pathname=path

An example


<
html>
   <
body>
      <script type="text/javascript">
          
document.write(location.pathname);     

      </script>
   </body>
</
html>

Suppose the current URL is: http://xforsys.com/example.htm#test5

Then the output of the above program is


/xforsys.com/example.htm

The location.pathname in document.write takes the pathname specified in the current URL and displays it with the output as displayed above.

{mospagebreak title=Properties : port, protocol and search}

port:

The hostname property for returning the hostname alone of the current URL is similar to the port property. The port property of location object is used for setting or returning the port number of the current URL. This port number refers to the communications port used by the server.

General syntax of port property of location Object:


location.port=portnumber

for example


<
html>
   <
body>
      <script type="text/javascript">
          
document.write(location.port);     

      </script>
   </body>
</
html>

Then the output of the above program is


24

The number 24 displayed above specifies the port number of the current URL.

protocol:

As the name of this property suggests, the protocol property of location object is used for setting or returning the protocol of the current URL.

General syntax of protocol property of location Object:


location.protocol=path

for example


<
html>
   <
body>
      <script type="text/javascript">
          
document.write(location.protocol);     

      </script>
   </body>
</
html>

Suppose the current URL is: http://xforsys.com/example.htm#test5

Then the output of the above program is


http:

The location.protocol in document.write takes the protocol specified in the current URL which is http: and displays it as shown above.

search:

The search property of the location object is a string and it is used to set and return the value from the current URL starting from question mark(?)

General syntax of search property of location Object:


location.search=path

In the above, the path is taken from question mark(?) of the current URL.

for example


<
html>
   <
body>
      <script type="text/javascript">
          
document.write(location.search);     

      </script>
   </body>
</
html>

Suppose the current URL is: http://xforsys.com/example.asp?filename=test10

Then the output of the above program is


?filename=test10

The location.search in document.write takes the symbol starting from ? in the current URL and displays it giving the output as ?filename=test10

[catlist id=157].

Related posts