tags:

views:

40

answers:

3

In the past I noticed that in many sites when you type a link with different arguments something else happens ( ex Google ). I would like to implement that into my site but I don't know how. I would like to do it for the search function to be exact. Let's say my site is test.com. The link test.com/find+item would search the site for the "find item" term. I'm using JQuery and JSON for the site. Also the search box is the 'input#suggestBox' element and $('input#suggestBox').jsonSuggest(bookData.webSites, {onSelect:callback}); the call for the search.

+4  A: 

Many sites, even this one, use a technique called URL rewriting. It basically takes a URL and rearranges it into a format your scripts can understand.

For example, you might set up a rule so that the URL http://example.com/search/foo gets rewritten to http://example.com/search.php?query=foo.

If you are using Apache, there is a module called mod_rewrite which handles this for you. You can find out more about it on the Apache documentation page or from this excellent question and answer from Owen.

nickf
A: 

I wouldn't advise using an ajax approach for getting search results on your site.

The reason for this is that if you have someone who wants to take the search results for a particular query on your site, they have no way of actually taking that link and posting it somewhere, and it decreases the possibility of inbound links to your site.

Instead, you should be using a simple form which has a text field but uses a GET action which would then post the values of the form to the query string.

Then, on the server side, you process the request like you would for any other dynamic request to that url.

Of course, how you do that depends completely on the technology you are using on the back end. The point is, this isn't so much about jQuery as it is about properly structuring the URLs on your site to provide functionality as well as discoverability.

casperOne
Although I generally agree with you, I disagree with the stated non-bookmarkability of these queries. You can still change the hash part of the url on-the-fly, which can be bookmarked, sent by e-mail, etc. Example: Gmail.
Marcel Korpel
@Marcel Korpel: I agree, it all depends on the way that the link is utilized by the poster. If the poster only uses the link for an ajax call to retrieve results, then that link and the result sets aren't going to be exposed to the user. This prevents discoverability. If the poster is just going to use jQuery to fabricate a link which is then used as a redirect, then using a FORM element with an action of GET does exactly what he wants, and he gets the discoverability as well.
casperOne
+1  A: 

The technique you are referring to is very closely related to the concept of RESTfulness. nickf's recommendation of using Apache's mod_rewrite is solid, but if you are wanting to really sink your teeth into URL-driven web app methods, I strongly recommend researching REST.

Anthony