javaScript location object

the window.location object contains information about the current url .

the window.location returns a location object, which contains information about the url of the document and provides methods for changing that url.

the window.location object can be written without the window perfix.

syntax:

          var    url_info   =   window.location

JavaScript window location object properties

the window.location object properties can be used to get information about the current url or set them to navigate to another url.

the default property for the window.location object is location.href.

propertydescription
hashsets or returns the subsection of the href property that follows the sign(#)
hostsets or returns the hostname and port of the location or the url.
hostnamesets or returns the host name part of the location or url
hrefsets or returns the entire url as a string
pathnamesets or returns the file name or path specified by the object
portsets or returns the port number associated with a url
protocolsets or returns the protocol portion of a url
searchsets or returns the substring of the href property that follows the mark(?)

example :
display the properties of this url(http://www.example.com:8080/search?q=devmo#test).

function     property(){
                                                                                   // set the new  url                                                
   window.location  =  "http://www.example.com:8080/search?q=devmo#test"; 
                     
   newlocation  =  window.location;        //retrieves the location  object
                     
   document.write("<br/>"+"hash      ="+newlocation.hash) ;
                     
   document.write("<br/>"+"host      ="+newlocation.host) ; 

   document.write("<br/>"+"hostname  ="+newlocation.hostname) ;
                     
   document.write("<br/>"+"href      ="+newlocation.href) ;
                     
   document.write("<br/>"+"pathname  ="+newlocation.pathname);
                     
   document.write("<br/>"+"port      ="+newlocation.port);
                     
   document.write("<br/>"+"protocol  ="+newlocation.protocol) ;
                     
   document.write("<br/>"+"search    ="+newlocation.search);
   
     }

output:     url(http://www.example.com:8080/search?q=devmo#test)

                   hash = #test 

                   host = www.example.com:8080
                                   
                   hostname = www.example.com
                                   
                   href     = http://www.example.com:8080/search?q=devmo#test
                                   
                   pathname = /search
                                   
                   port     = 8080
                                  
                   protocol = http:
                                   
                   search   = ?q=devmo

JavaScript window.location object methods

the location.assign() method loads a new html document.

syntax:

          window.location.assign(stringurl)
  • stringurl : string that specifies the url of the document to load.
 function   assign(){
       
       
      window.location.assign("http://www.google.co.in/?gws_rd=cr#bav=on.2");
                  
                  }
                  
 function    load(){
      
                  window.location  = "http://www.yahoo.com/";
                 
 // you can assign a url string to location property, the browser interpret string as a url (uniform resource locator) 

and attempts to load and display the document at that url(uniform resource locator)
}

location object reload()  method

the location.reload() method reloads the current document.

syntax:

          location.reload(true |  false)
  • false : default.reloads the document from the cache
  • true : reloads the document from the server

example : force reloading the current page from the server.

          function    reload(){
          
                                window.location.reload(true);
                                
                             }   

location object replace() method

the location.replace() method replaces the current document by loading another document at the specified url.

syntax:

          location.replace(newurl)
  • newurl : string that specifies the url .
   function  replace(){
   
                              window.location.replace("http://www.w3webtutorial.com") ;
                        
                    }