JavaScript | window.location and document.location Objects Last Updated : 13 May, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 <!DOCTYPE html> <html lang="en"> <head> <title>Get Different Part of a URL</title> </head> <body> <script> // Prints complete URL document.write("URL IS: " + window.location.href + "<br>"); // Prints hostname like local host (www.example.com) document.write("HOSTNAME: " + window.location.hostname + "<br>"); // Prints pathname like /products/find.php document.write("PATHNAME: " + window.location.pathname + "<br>"); // Prints the protocol used like http: or https: document.write("PROTOCOL: " + window.location.protocol + "<br>"); // Prints hostname with port like localhost:3000 document.write("HOSTNAME WITH PORT: " + window.location.host + "<br>"); // Prints port number like 3000 document.write("PORTNUMBER: " + window.location.port + "<br>"); </script> </body> </html> Output: URL IS: https://ide.geeksforgeeks.org/tryit.php HOSTNAME: ide.geeksforgeeks.org PATHNAME: /tryit.php PROTOCOL: https: HOSTNAME WITH PORT: ide.geeksforgeeks.org PORTNUMBER: Note: When you visit a specific website, it is always connected to a port. However, most browsers will not display the default port number. Example 2: Assign or load new document. javascript <!DOCTYPE html> <html lang="en"> <head> <title> Load another Resource or document from a URL </title> <script> function loadPage() { window.location.assign( "https://ide.geeksforgeeks.org"); } </script> </head> <body> <button type="button" onclick="loadPage();"> Load Page </button> </body> </html> Output: Before Clicking the Button: After Clicking the Button: Comment More infoAdvertise with us Next Article Javascript Window Open() & Window Close() Method S singhgaya101 Follow Improve Article Tags : JavaScript Web Technologies javascript-object Similar Reads How to set location and location.href using JavaScript ? In this article, we will set the location and location.href using Javascript.Both location and location.href are used to set or return the complete URL of your current page. They return a string that contains the entire URL with the protocol.Syntax:location = "https://www.geeksforgeeks.org";orlocati 1 min read JavaScript location.host vs location.hostname In this article, we will learn about how to get information related to the webpage like hostname, port number, etc. We will also see the difference between the location.hostname and location.host.location object is used to get information about the current web page. host and hostname are properties 2 min read Window Object in JavaScript In JavaScript, the Window object represents the browser window that contains a DOM document.The Window object offers various properties and methods that enable interaction with the browser environment, including manipulating the document, handling events, managing timers, displaying dialog boxes, an 5 min read Difference between window.location.href, window.location.replace and window.location.assign in JavaScript Window.location is a property that returns a Location object with information about the document's current location. This Location object represents the location (URL) of the object it is linked to i.e. holds all the information about the current document location (host, href, port, protocol, etc.)A 4 min read Javascript Window Open() & Window Close() Method The Javascript Window.Open() method is used to open the web pages into a new window or a new tab. It depends on the browser settings and the values assigned to the parameter. Syntax:window.open(URL, name, specs, replace)Parameters: This method accepts four parameters as mentioned above and described 3 min read HTML DOM Window.location Property HTML DOM Window.location property returns a Location object that contains information about the current location of the document. Window.location is read-only property. Syntax: var loc = window.location; Return Value: An object containing information about the location of the document. Example: This 1 min read Like