JavaScript | window.location and document.location Objects Last Updated : 13 May, 2020 Comments Improve Suggest changes 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.location and document.location Objects 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 How are the JavaScript window and JavaScript document different from one another? What is a JavaScript window? The window is at a root/top level at the JavaScript object hierarchy. It is a global/root object in JavaScript and it is the root object of the Document object model(DOM); What is a JavaScript document? A document is an object inside the window object and we use a docume 3 min read HTML DOM Location Object The Location Object in HTML DOM is used to provide information about the current URL of a webpage. This object is a sub part of a window object. As it get the information by using window.location property. The location Object is used to retrieve the information regarding the hash value , host _name 3 min read How to get protocol, domain and port from URL using JavaScript ? The protocol, domain, and port of the current page can be found by two methods: Method 1: Using location.protocol, location.hostname, location.port methods: The location interface has various methods that can be used to return the required properties. The location.protocol property is used to return 2 min read How to remove hash from window.location with JavaScript without page refresh ? The replaceState() method is used to modify the current history entry, replacing it with the state objects, title, and URL passed in the method parameters. This method is useful when you want to update the state object or URL of the current history entry in response to some user action. To remove th 1 min read Like