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.


          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.

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(

function     property(){
                                                                                   // set the new  url                                                
   window.location  =  ""; 
   newlocation  =  window.location;        //retrieves the location  object
   document.write("<br/>"+"hash      ="+newlocation.hash) ;
   document.write("<br/>"+"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    =";

output:     url(

                   hash = #test 

                   host =
                   hostname =
                   href     =
                   pathname = /search
                   port     = 8080
                   protocol = http:
                   search   = ?q=devmo

JavaScript window.location object methods

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


  • stringurl : string that specifies the url of the document to load.
 function   assign(){
 function    load(){
                  window.location  = "";
 // 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.


          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(){

location object replace() method

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


  • newurl : string that specifies the url .
   function  replace(){
                              window.location.replace("") ;