Forum Moderators: open

Message Too Old, No Replies

Search function for website

need to be able to search my site

         

cja2882

5:57 am on Jul 17, 2004 (gmt 0)



Can anyone tell me how to add a search function to my website so that the user can search my site and find what they are looking for?

Bernard Marx

11:17 am on Jul 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi cja,

Ultimately, this is best done with some 'back-end' stuff on your own server. It's not within client-side capabilities. However, it's possible to get Google to search your site for you.

When you want to restrict your search to a particular domain, you do this:

some search terms site:www.somesite.com

Try it in google to see what the results are for your own site. You'll see how the information is put together in a query string in the URL in the address bar, with some characters escaped, so you can see how to create your own queries. I have removed some of the language specifics, and cut it down to basics for this example:

<html>
<head>
<title>site search</title>

<script>
function search()
{
/* constants */
var google = "http://www.google.com/search";
var site = "+site%3Awww.webmasterworld.com";
/* search string */
var qString = "?q="+escape(document.getElementById("searchbox").value);
var loc = google + qString + site;
window.location = loc;
}
</script>

</head>
<body>
<input id="searchbox" type="text" size="30">
<button onclick="search()">search site</button>
</body>
</html>

Of course, you are relying on Google being up-to-date as regards your site. Not much you can do about that.

The only other alternative, without using server-side scripts, is to manually compile information (keywords etc) about your site, and put the information in a structured format in a javascript file.

claus

12:24 pm on Jul 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld cja :)

You might try searching the web for "Atomz" - it's a search engine that you can use with your site. There are more like it, but this one lets you cusomize the look and generates reports about the things people search for - for a small site (<500 pages) it is free.

There's also the Google site search: [google.com...]

Javascript is not the right tool to use for that job ;)