Use Javascript To Get Current URL Page

I have some custom javascript functions that are included on all of my web pages; however, a few functions are only intended for the home page. So I decided to use the “window.location” object to determine if the site URL is in fact the home. See example below:

if(window.location==”http://domain.com/”){
// run home page functions
}

Here are a few other helpful JavaScript objects that expose site locations:

  1. hash: Returns the anchor portion of a URL
  2. host: Returns the hostname and port of a URL
  3. hostname: Returns the hostname of a URL
  4. href: Returns the entire URL
  5. pathname: Returns the path name of a URL
  6. port: Returns the port number the server uses for a URL
  7. protocol: Returns the protocol of a URL
  8. search: Returns the query portion of a URL

You may also like...

Leave a Reply