JavaScript | window.location and document.location Objects (original) (raw)
Last Updated : 13 May, 2020
window.location and document.location: These objects are used for getting the URL (the current or present page address) and avert browser to a new page or window. The main difference between both is their compatibility with the browsers.
- The window.location is read/write on all compliant browsers.
- The document.location is read-only in Internet Explorer but read/write in Firefox, SeaMonkey that are Gecko-based browsers.
All modern browsers map document.location to the window.location but you can prefer window.location for cross-browser safety.Syntax:
- window.location.href: It returns the URL of the current working page.
- window.location.hostname: It returns the domain name of web host.
- window.location.pathname: It returns the path and filename of the current working page.
- window.location.protocol: It returns the used protocol (http: or https:).
- window.location.port(): It prints the port number.
- window.location.host(): It prints host name along with port number.
- window.location.assign(): It loads new document.
Example 1: This example using different properties to get different parts of URL.
javascript `
Get Different Part of a URL