Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract word from Url

I have a search function, and would like to display the search term in the search input.

My url is: search-1.html?keyword=XXXXXX

How do I get this, and display it in an input?

Thank you in advance.

like image 896
curly_brackets Avatar asked May 17 '11 21:05

curly_brackets


2 Answers

Use this: http://ajaxcssblog.com/jquery/url-read-get-variables/

Take luck!

Oh and then you can use the following to display its value in an input field:

$("#inputId").val($.url.param("keyword"));
like image 90
Todd Avatar answered Sep 24 '22 20:09

Todd


If it is just one key=value in the url you can use simple regex like this:

var theValueYouWant = window.location.href.match(/keyword=(.+)/)[1]

And set the value of an input like this

$('input').val(theValueYouWant)

If you want to parse the GET string more thoroughly, this function should do it...

gets = {};
$.each(location.search.replace(/^\?/,'').split('&'),function(k,v){
    bits = v.split('=');
    gets[bits[0]] = bits[1];
});
like image 24
Billy Moon Avatar answered Sep 21 '22 20:09

Billy Moon