page url javascript code example

Example 1: get actual url in variable

var currentLocation = window.location;

Example 2: javascript get domain

window.location.hostname

Example 3: javascript hashtag url

var hash = window.location.hash;
if (hash) {
  // Fragment exists
  hash = hash.substring(1);
  console.log(hash);
} else {
  // Fragment doesn't exist
}

Example 4: javascript url

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript</h2>

<h3>The window.location object</h3>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 
"The full URL of this page is:<br>" + window.location.href;
</script>

</body>
</html>