2

Possible Duplicate:
Get query string values in JavaScript

I am writing a jQuery function that saves an item remotely with Ajax. I'd like to be able to grab the edit.php?id=###&field=### info from the url right in my function. How would I do that?

Community
  • 1
  • 1
Damon
  • 10,493
  • 16
  • 86
  • 144
  • 1
    this post explains how to do it [get-query-string-values-in-javascript][1] [1]: http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript – Dmitry Samuylov Aug 29 '11 at 19:15

1 Answers1

3

You don't need jQuery for this task, you can do it simply with javascript :

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];
}

Then if you retrieve this url : http://www.yoursite.com/Index.php?g=12&param=43 to retrieve the 'param' parameter, all you need to do is this :

var param_value = gup('param');

or the 'g' parameter like this :

var param_value = gup('g');

or switch 'param' with the name of the parameter you need.

gillyb
  • 8,760
  • 8
  • 53
  • 80