Open In App

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

Improve
Improve
Like Article
Like
Save
Share
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:


Last Updated : 31 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads