JavaScript window.location

 

ATTENTION: THIS PAGE IS Valid HTML 5 AND IS BEST VIEWED WITH HTML 5 - Please upgrade your browser or download one of the HTML 5 compatible browsers such as Mozilla Firefox, Chrome, Opera or IE 9 (March 14, 2011 or later). For more information see HTML 5 browsers.


If you find this helpful, please click the Google +1 Button to the left, if it is white, to make it turn blue or red. Thank you! (It also helps find this page again more easily.)


PDF mobile

JavaScript window.location

window.location properties
Try the following URL for some good examples below: window.location properties
Note: due to use of the explicit port number ":80" in that URL, some browsers will incorrectly report that URL as a potential phishing site and then remove the port number, so the port number may be missing from window.location.port and the values of window.location.href and window.location.host.
window.location propertyUse / Result
get or setStringwindow.location.href
window.location.href = value
the entire URL of the current document
Example:
get or setStringwindow.location.protocol
window.location.protocol = value
the protocol scheme (preceding the "//") from the URL of the current document, including the colon (":")
Example:
get or setStringwindow.location.host
window.location.host = value
the host name or IP address and port number from the URL of the current document
Example:
get or setStringwindow.location.hostname
window.location.hostname = value
the host name or IP address from the URL of the current document
Example:
get or setStringwindow.location.port
window.location.port = value
the port number from the URL of the current document; none indicates it is the default HTTP port number 80
Example:
get or setStringwindow.location.pathname
window.location.pathname = value
the path name from the URL of the current document, starting with the leading slash ("/") that initiates an absolute URL
Example:
Example:
get or setStringwindow.location.hash
window.location.hash = value
the fragment identifier from the URL of the current document, starting with the hash symbol ("#")
Example:
window.location functions
window.location functionUse / Result
Stringwindow.location = url;
window.location.assign(url)
load the document at url and keep the current document in the session history
Example:window.location.assign("/javascript/objects/")
Stringwindow.location.reload()
window.location.reload(forceget)
reload the current document, possibly from cache unless forceget == true
Example:window.location.reload(false)
Stringwindow.location.replace(url)load the document at url and replace the current document in the session history
Example:window.location.replace("/javascript/objects/")

Valid HTML 5