14

Possible Duplicate:
get querystring with jQuery

How do I get the value of a querystring into a textbox using jQuery?

Lets say the url is http://intranet/page1.php?q=hello

I would like the "hello" to be in the textbox.

Community
  • 1
  • 1
oshirowanen
  • 15,505
  • 80
  • 191
  • 340
  • See this question: http://stackoverflow.com/q/901115/140185 and this one: http://stackoverflow.com/q/585852/140185 – Manoj Govindan Sep 24 '10 at 14:40
  • If you're passing GET variables, what server-side script language are you using? If you have a server-side script available, then it'd probably be easier to use PHP (or whatever else) to populate the `textarea` than js/jQuery. – David Thomas Sep 24 '10 at 15:09

2 Answers2

31

In my programming archive I have this function:

function querystring(key) {
   var re=new RegExp('(?:\\?|&)'+key+'=(.*?)(?=&|$)','gi');
   var r=[], m;
   while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
   return r;
}

You can use that to get the query string value and put in a textbox:

$('#SomeTextbox').val(querystring('q'));
dav_i
  • 26,475
  • 17
  • 99
  • 133
Guffa
  • 666,277
  • 106
  • 705
  • 986
8

Use the function listed in the answer to this question:

function getParameterByName( 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 decodeURIComponent(results[1].replace(/\+/g, " "));
}

And then just do something like this:

var qParam = getParameterByName('q');
$('#mytextbox').val(qParam);
Community
  • 1
  • 1
Aistina
  • 12,095
  • 12
  • 67
  • 87