Location

JavaScript Location Object is an build in object having information regarding URL of webpage page. location object shows properties like url path, protocol, ip address, port no etc by using window.location property. It returns read only properties.


    document.write(window.location);

JS Location Properties and Methods

Properties and Methods of JavaScript Location

  1. href
  2. origin
  3. host
  4. hostname
  5. assign
  6. hash
  7. pathname
  8. port
  9. protocol
  10. search
  11. reload

href

location.href property returns full URL as string. This includes protocol, domain, pathname, hash etc. See example


    location.href;

origin

location.origin property returns protocol and domain name of URL as string. See example


    location.origin;

host

location.host property returns only domain name as string. If web application is running on IP Address, then IP with port no of URL will be displayed as string . See example


    location.host;

hostname

location.host property returns only domain name as string. If web application is running on IP Address, then only IP address will be displayed as string. See example


    location.hostname;

assign

location.assign method is used to load another URL in same tab. See example


    location.assign("https://www.techaltum.com");

hash

location.hash property returns a string that starts with "#". usually hash "#" is used in URLs to fetch Internal Link created using ID attribute. See example


    location.hash;


pathname

location.pathname property returns a string of path. Usually path is either filename with extension or directory. See example


    location.pathname;

port

location.port property returns a string of port no of IP Address. For domains, this string will be empty. See example


    location.port;

protocol

location.protocol property returns a string of protocol of URL. Commonly used protocols are http, https, file, ftp. See example


    location.protocol;

location.search property returns a string stared by ? and then follow by query string parameter in URL. See example


   https://www.techaltum.com/search?q=javascript
   
    location.search;  // return "?q=javascript"

reload

location.protocol methods is used reload current browser tab. There can be parameter true in search method to reload page from server without cache. See example




    location.reload();        // reload page with cache
    location.reload(true);    // reload page without cache