The Wayback Machine - https://web.archive.org/web/20240118173330/https://www.geeksforgeeks.org/how-to-read-a-hash-with-an-sign-in-the-url/
Open In App
Related Articles

How to read a hash with an “&” sign in the URL ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

It’s a very common situation in web development where users want to extract or read information from any URL. In the case of the unavailability of the server-side code, the programmer can make use of JavaScript or jQuery to read information. To read the URL with an & sign, we can use the split() function. By that function, we can easily get the URL parameter as a string. The programmer can retrieve the parameter string and store it in a variable by using JavaScript.

Syntax:

var q = url.split('?')[1];

Here URL is any example URL string. To get URL query string parameters. If the URL has many parameters in the query string, the following code snippet will guide you to parse and store in variables that can be accessed.

var vars = [], hash;
var q = url.split('?')[1];
var fullParameter = q;
            
if(q != undefined) {
    q = q.split('&');
                
    for(var i = 0; i < q.length; i++) {
        hash = q[i].split('=');
        vars.push(hash[1]);
        vars[hash[0]] = hash[1];                
    }
            
    // Get hash parameter
    var hashParameter = hash[1];
    var hashstr= hashParameter.split('#')[1];
    alert("# Parameter :" + hashstr);
}

To use any parameters, the user can access the value by parameter name. For example, if the URL contains the query string “?var1=name&var=surname&var3=address?, the user can access the value for “var1” using:

alert(vars['var1']);

Below example illustrates the above approach:

Example: This example reads URL parameters which includes hash along with "&" sign in the given URL.




<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to read a hash with
        an & sign in the URL?
    </title>
      
    <script src=
    </script>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        Get parameters and anchor
        of URL using jQuery
    </b>
      
    <p>
        Click on the button below to
        read parameters for any URL
    </p>
  
    <button id="btn">
        Read hash with & sign:
    </button>
      
    <div style="height:10px;"></div>
  
    <div id="Ampdiv"></div>
      
    <script>
        var url = 
        $('#btn').click(function() {
            readHash(url);
        });
  
        function readHash(url) {
            var vars = [], hash;
            var q = url.split('?')[1];
            var fullParameter = q;
  
            if (q != undefined) {
                q = q.split('&');
  
                for (var i = 0; i < q.length; i++) {
                    hash = q[i].split('=');
                    vars.push(hash[1]);
                    vars[hash[0]] = hash[1];
                }
                  
                // Get hash parameter
                var hashParameter = hash[1];
                var hashstr = hashParameter.split('#')[1];
                alert("# Parameter :" + hashstr);
            }
              
            $("#Ampdiv").text(fullParameter);
        }
    </script>
</body>
  
</html>


Output:

  • Before clicking the button:
  • After clicking the button:

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 31 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials