I use url rewriting on one of my projects, and I would like to be able to perform queries through a search box with javascript by, on a "type enter" event, grabbing the value of this search box and passing it through a GET request to another search page. The end result would be a search address a la twitter (ie mysite.com/search/mykeyword). My code is the following:
$("#search-box").keyup(function(event) {
var code = (event.keyCode ? event.keyCode : event.which);
if (code == "13") { // ENTER
var q = $(this).val();
document.location.href = "/search/"+q;
}
});
2 questions:
Thanks
Don't use escape. Use encodeURIComponent
.
var q = encodeURIComponent($(this).val());
Perfectly fine... although I wouldn't use URL rewriting on the parameters to a search.