Javascript – Get the Anchor Part of the URL

The following example shows you how to get the anchor part of the URL in Javascript.

<html>
  <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        // URL = http://eureka.ykyuen.info#Welcome%20to%20Eureka!
        
        alert(self.document.location.hash);
        // Output: #Welcome%20to%20Eureka!
        
        alert(self.document.location.hash.substring(1));
        // Output: Welcome%20to%20Eureka!
        
        alert(unescape(self.document.location.hash));
        // Output: #Welcome to Eureka!
        
        alert(unescape(self.document.location.hash.substring(1)));
        // Output: Welcome to Eureka!
      });
    </script>
  </head>
  <body>
    <div id="eureka-link">
      <a href="http://eureka.ykyuen.info">Eureka!</a>
    </div>
  </body>
</html>

 

Done =)

Reference: Reading URL anchor refrence

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.