Javascript window location object properties contain information about the current URL. For example” location.pathname” returns “/subdirectory/search.”

Example URL: http://www.domain.com/subdirectory/search?filter=a#somewhere

Javascript Window Location Object Properties

PropertyDescriptionReturns
hashReturns the anchor portion of a URL#somewhere
hostReturns the hostname and port of a URLwww.domain.com (will include port if one exists)
hostnameReturns the hostname of a URLwww.domain.com
hrefReturns the entire URLhttp://www.domain.com/subdirectory/search?filter=a#somewhere
pathnameReturns the path name of a URL/subdirectory/search
portReturns the port number the server uses for a URL(empty string)
protocolReturns the protocol of a URLhttp:
searchReturns the query portion of a URL?filter=a

Javascript Window Location Object Properties Alert

   <script type="text/javascript">
        //note some parts may be blank depending on your URL
        alert("hash: " + location.hash + "\n" +
        "host: " + location.host + "\n" +
        "hostname: " + location.hostname + "\n" +
        "href: " + location.href + "\n" +
        "pathname: " + location.pathname + "\n" +
        "port: " + location.port + "\n" +
        "protocol: " + location.protocol + "\n" +
        "search: " + location.search );  
    </script>

LEAVE A REPLY

Please enter your comment!
Please enter your name here