Exforsys.com
 
Home Tutorials JavaScript
 

JavaScript Location Object Properties

 

Properties: hostname, href and pathname

Page 2 of 3


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.




Next Page: Properties : port, protocol and search


Read Next: JavaScript Location Object



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape