- JavaScript Basics
- JavaScript Home
- JavaScript Syntax
- JavaScript Placements
- JavaScript Output
- JavaScript Statements
- JavaScript Keywords
- JavaScript Comments
- JavaScript Variables
- JavaScript var
- JavaScript let
- JavaScript const
- JavaScript var Vs let Vs const
- JavaScript Operators
- JavaScript Arithmetic Operators
- JavaScript Assignment Operators
- JS Comparison Operators
- JavaScript Logical Operators
- JavaScript Bitwise Operators
- JavaScript Ternary Operator
- JavaScript Operator Precedence
- JavaScript Data Types
- JavaScript typeof Operator
- JS Conditional Statements
- JavaScript Conditional Statement
- JavaScript if Statement
- JavaScript if...else Statement
- JavaScript switch Statement
- JavaScript Loops
- JavaScript for Loop
- JavaScript while Loop
- JavaScript do...while Loop
- JavaScript break Statement
- JavaScript continue Statement
- JavaScript break Vs. continue
- JavaScript Popup Boxes
- JavaScript Dialog Box
- JavaScript alert Box
- JavaScript confirm Box
- JavaScript prompt Box
- JavaScript Functions
- JavaScript Functions
- JS Function with Parameter
- JavaScript Return Statement
- JavaScript Variable Scope
- JavaScript setTimeout() Method
- JavaScript setInterval() Method
- JavaScript Events
- JavaScript Events
- JavaScript onclick Event
- JavaScript onload Event
- JavaScript Mouse Events
- JavaScript onreset Event
- JavaScript onsubmit Event
- JavaScript Objects
- JavaScript Objects
- JavaScript Number Object
- JavaScript Array Object
- JavaScript String Object
- JavaScript Boolean Object
- JavaScript Math Object
- JavaScript RegExp Object
- JavaScript Date Object
- JavaScript Browser Objects
- JavaScript Browser Objects
- JavaScript Window Object
- JavaScript Navigator Object
- JavaScript History Object
- JavaScript Screen Object
- JavaScript Location Object
- JavaScript Document Object
- JS Document Object Collection
- JS Document Object Properties
- JS Document Object Methods
- JS Document Object with Forms
- JavaScript DOM
- JavaScript DOM
- JavaScript DOM Nodes
- JavaScript DOM Levels
- JavaScript DOM Interfaces
- JavaScript Cookies
- JavaScript Cookies
- JavaScript Create/Delete Cookies
- JavaScript Advance
- JavaScript Regular Expression
- JavaScript Page Redirection
- JavaScript Form Validation
- JavaScript Validations
- JavaScript Error Handling
- JavaScript Exception Handling
- JavaScript try-catch throw finally
- JavaScript onerror Event
- JavaScript Multimedia
- JavaScript Animation
- JavaScript Image Map
- JavaScript Debugging
- JavaScript Browser Detection
- JavaScript Security
- JavaScript Misc
- JavaScript innerHTML
- JavaScript getElementById()
- JS getElementsByClassName()
- JS getElementsByName()
- JS getElementsByTagName()
- JavaScript querySelector()
- JavaScript querySelectorAll()
- JavaScript document.write()
- JavaScript console.log()
- JavaScript instanceof
- JavaScript Programs
- JavaScript Programs
- JavaScript Test
- JavaScript Online Test
- Give Online Test
- All Test List
JavaScript Location Object
The location object in JavaScript helps in storing the information of current URL of the window object. It is a child object of the window object.
The location object has the following two features:
- Properties
- Methods
JavaScript Location Object Properties
The table given below describes the properties of the Location object in JavaScript.
Property | Description | Example |
---|---|---|
href | represents a string specifying the entire URL | http://codescracker.com:80?test.asp?id=1#start |
protocol | represents a string at the beginning of a URL up to the first colon (:), which specifies the method of access to the URL | http: or https: |
host | represents a string consisting of the hostname and port strings | codescracker.com:80 |
hostname | represents the server name, subdomain, and domain name of a URL | codescracker.com |
port | represents a string specifying the communications port that the server uses | 80 |
pathname | represents a string portion of a URL specifying how a particular resource can be accessed | test.asp |
search | represents a string beginning with a question mark that specifies any query information in an HTTP URL | id=1 |
hash | represents a string beginning with a # specifies an anchor name in an HTTP URL | start |
JavaScript Location Object Methods
The following table lists the methods of the Location object in JavaScript.
Method | Description |
---|---|
assign() | Loads a new document in the browser |
reload() | reloads the current document that is contained in the location.href property |
replace() | replaces the current document with the specified new one, you can not navigate back to the previous document using the browser's Back button |
JavaScript Location Object Example
Here is an example demonstrates location object in JavaScript:
<!DOCTYPE HTML> <html> <head> <title>JavaScript Location Object</title> <script type="text/javascript"> function gotoUrl() { window.location.href = window.document.loctn.ProtocolFld. options[window.document.loctn.ProtocolFld.selectedIndex]. text + document.loctn.HostnameFld.value + document.loctn. PathnameFld.value } </script> </head> <body> <h3>Enter URL in following sections</h3> <form name="loctn" method="post"> <pre>Protocol: <select name="ProtocolFld" size="1"> <option>http://</option> <option>file://</option> <option>javascript:</option> <option>ftp://</option> <option>mailto:</option> </select> </pre> <pre> Hostname: <input type="text" size="20" maxlength="256" name="HostnameFld" value="codescracker.com"> </pre> <pre> Pathname: <input type="text" size="20" maxlength="100" name="PathnameFld" value="/"> </pre> <pre> <input type="button" name="Go" value="Go" onclick="gotoUrl()"> </pre> </form> </body> </html>
Here is the sample output of the above JavaScript location object example code:

Now fill (or edit and fill) the required field as shown in the following snapshot:

After filling the required field as shown in the above figure, click on Go button to go to that url. Here is the sample output after clicking on the Go button:

« Previous Tutorial Next Tutorial »