How can you check for a #hash in a URL using JavaScript? How can you check for a #hash in a URL using JavaScript? jquery jquery

How can you check for a #hash in a URL using JavaScript?


Simple use of location hash:

if(window.location.hash) {  // Fragment exists} else {  // Fragment doesn't exist}


  if(window.location.hash) {      var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character      alert (hash);      // hash found  } else {      // No hash found  }


Put the following:

<script type="text/javascript">    if (location.href.indexOf("#") != -1) {        // Your code in here accessing the string like this        // location.href.substr(location.href.indexOf("#"))    }</script>