Automatically inserting window.location.hash into html input value?

Tags: ,



Here’s what I’m essentially trying to do:

<input type="text" class="form-control" value="window.location.hash">

What’s the proper way to insert the window.location.hash into the input‘s value?

Note: I’ve found several ways to do this when people are required to click a button, but nothing that explains how to do it automatically when the page loads.

Answer

You’ll need to assign this after the page loads, or at least the element

<input type="text" class="form-control" id="hash" value="">
<script>
  window.onload=function() {
     document.querySelector("#hash").value = window.location.hash
  }
</script>


Source: stackoverflow