Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery querystring [duplicate]

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.

like image 589
oshirowanen Avatar asked Sep 24 '10 14:09

oshirowanen


2 Answers

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'));
like image 129
Guffa Avatar answered Nov 03 '22 14:11

Guffa


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);
like image 33
Aistina Avatar answered Nov 03 '22 15:11

Aistina