0

I am able to get data from a URL with the following structure -

http://mydomain.com/test.php?word=hello

Using PHP I would use the following

$word = $_GET["word"];

Can anyone tell me if it is possible to achieve the same thing but using JavaScript?

Thanks

Rory Standley
  • 1,130
  • 2
  • 20
  • 38

2 Answers2

1

Yes, it is possible:

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

Source: Get URL Parameters Using Javascript

Sascha Galley
  • 15,041
  • 5
  • 36
  • 51
0

You can get these values using window.location.search. You can either roll your own code to parse this or use something like the answers to this question.

Community
  • 1
  • 1
Manu Clementz
  • 1,767
  • 12
  • 19