0

I have an application need to parse the parameter from URL. But the URL got special character '#'.

URL: http://localhost:8080/test.html?parms=PESTL4#2

I printed the windows.location, and the result is "http://localhost:8080/test.html?parms=PESTL4#2".

I used the following sample code to get parms, and it returns PESTL4. How does it work?

function getQueryVariable(variable)
{
   var query = window.location.search.substring(1);
   var vars = query.split("&");
   for (var i=0;i<vars.length;i++) {
           var pair = vars[i].split("=");
           if(pair[0] == variable){return pair[1];}
   }
   return(false);
}
user3738211
  • 49
  • 2
  • 7

3 Answers3

0

window.location.search returns query string ?parms=PESTL4 without #2 first line removes '?'.

and then split query string to pairs so name=joe&age=18 to ['name=joe','age=18']

in for loop it splits string to key value name=joe to ['name','joe'].

and if first (name) equals variable return the second joe

Omar Elawady
  • 3,300
  • 1
  • 13
  • 17
0

All that you need are places in document.location object.

Localiton Object Documentation

Dmitry Zaets
  • 3,289
  • 1
  • 20
  • 33
0

As in their example: Get url parameter jquery Or How to Get Query String Values In js

var tech = getUrlParameter('technology');
var blog = getUrlParameter('blog');
Community
  • 1
  • 1
Angry 84
  • 2,935
  • 1
  • 25
  • 24