0

I am trying to create a web app that uses a zip code (passed from the previous page). The URL looks like /location#12345

I am trying to store everything after '#' in a variable to be used in the location finder app.

        URL: /locations#12345

        $(document).ready(function(){
            var queryString = decodeURIComponent(window.location.search);
            queryString = queryString.substring(1);
            var queries = queryString.split("#");
            var myVal;
            for (var i = 0; i < queries.length; i++)
            {
                myVal = myVal + queries[i];    
            }
            alert(myVal);
        });

I thought alert(myVal) would produce "12345" but it is blank. There are no errors in the console.

Andrew
  • 379
  • 1
  • 2
  • 13

1 Answers1

0

For a single hash String.split() is enough:

Working demo

const str = '/locations#12345UR';
const hash = str.split('#')[1];
alert(hash);
        
Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43