How to get current URL with JavaScript?
window.location.href JavaScript provides you with different methods to get and set the current URL , which is displayed in the web-browser's address bar. All these methods use the Location object , which is a property of the Window object. Use its properties to get information on the current page address or use its methods to do some page redirect or refresh.
URL of this page is:
<!DOCTYPE html>
<html>
<body>
<p id="href">URL of this page is:</p>
<script type="text/javascript">
function getURL() {
document.getElementById("href").innerHTML =
"URL of this page is:<br>" + window.location.href;
}
</script>
<form id="b">
<input type="button" id="two" onclick="getURL()" value="Get URL of this page" />
</form>
</body>
</html>
URL structure
URL's are also called links. It is an address on the internet. It's made up of a protocol , domain name, and a path.
<protocol>//<hostname>:<port>/<pathname><search><hash>

JavaScript window.location properties
Using these window.location object properties you can access all of these URL components and what they can set or return:- href - Full URL (http://www.example.com:8082/index.php#tab2)
- protocol - Protocol Schema (http: or https)
- host - Domain name + port (www.example.com:8082)
- hostname - Domain name (www.example.com)
- port - Port Number (8082)
- pathname - the path name of the URL (index.php)
- search - ? followed by Query String (?foo=789)
- hash - # followed by the Anchor or Fragment identifier (#tab2)

window.location properties in javascript
<!DOCTYPE html>
<html>
<body>
<h2>window.location properties in javascript</h2>
<p id="href"></p>
<p id="protocol"></p>
<p id="host"></p>
<p id="hostname"></p>
<p id="port"></p>
<p id="pathname"></p>
<p id="search"></p>
<p id="hash"></p>
<script type="text/javascript">
function getURLProperties() {
document.getElementById("href").innerHTML =
"URL of this page is:<br>" + window.location.href;
document.getElementById("protocol").innerHTML =
"Protocol of this page is:<br>" + window.location.protocol;
document.getElementById("host").innerHTML =
"Host of this page is:<br>" + window.location.host;
document.getElementById("hostname").innerHTML =
"Hostname of this page is:<br>" + window.location.hostname;
document.getElementById("port").innerHTML =
"Port of this page is:<br>" + window.location.port;
document.getElementById("pathname").innerHTML =
"Pathname of this page is:<br>" + window.location.pathname;
document.getElementById("search").innerHTML =
"Search of this page is:<br>" + window.location.search;
document.getElementById("hash").innerHTML =
"Hash of this page is:<br>" + window.location.hash;
}
</script>
<form id="d">
<input type="button" id="four" onclick="getURLProperties()" value="Get URL Properties of this page" />
</form>
</body>
</html>
Related Topics
- JavaScript Popup Boxes
- Opening a new window in JavaScript
- How to Create Drop-Down Lists in JavaScript
- How do I include a JavaScript file in another JavaScript file?
- Print the content of a Div using JavaScript/jQuery
- How to Detect a Mobile Device with JavaScript/jQuery
- How to validate an email address in JavaScript
- JavaScript Array Iteration
- How to Remove a Specific Item from an Array in JavaScript
- What is JavaScript closures?
- How To Remove a Property from a JavaScript Object
- How to get selected value from Dropdown list in JavaScript
- How do I get the current date in JavaScript?
- How to Open URL in New Tab | JavaScript
- How to delay/wait/Sleep in code execution | JavaScript
- How to round to at most 2 decimal places | JavaScript
- How to convert string to boolean | JavaScript
- How to check undefined in JavaScript?
- How To Copy to Clipboard | JavaScript
- How to encode a URL using JavaScript?
- How to force Input field to enter numbers only | JavaScript
- How to create multiline string in JavaScript
- How to Check for an Empty String in JavaScript?