Link to home
Start Free TrialLog in
Avatar of Colly321
Colly321

asked on

Search Facility

Hi there

I want to know how I can allow a user to search the content of my Website without having to use server-side code.

The site has no database.

Is this possible? If so, how?

Cheers

Andy
Avatar of benwiggy
benwiggy
Flag of United Kingdom of Great Britain and Northern Ireland image

Javascript search engine:

form.html

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">

<html>
<head>
<title>JSE</title>

<script language="JavaScript1.3" type="text/javascript" src="jse_form.js">

/*
JSE Search Engine 1.0a- By Steve (www.tipue.com/products/jse)
Minor changes by JavaScriptKit.com- http://www.javascriptkit.com/script/script2/jse/
*/

</script>

</head>
<body>


<!-- Find -->

<form name="jse_Form" onsubmit="search_form(jse_Form);return false">
<input type="text" name="d">
<input type="button" value="Go" onclick="search_form(jse_Form)">
</form>

Enter JSE in the example above.
<p><font face="arial" size="-2">This free script provided by<br><a href="http://www.javascriptkit.com">JavaScript
Kit</a></font></p>


</body>
</html>

jse_form.js:

/ ---------- script properties ----------


var results_location = "results.html";


// ---------- end of script properties ----------


function search_form(jse_Form) {
      if (jse_Form.d.value.length > 0) {
            document.cookie = "d=" + escape(jse_Form.d.value);
            window.location = results_location;
      }
}

jse_search.js:


// ---------- script properties ----------

var include_num = 1;
var bold = 0;

// ---------- sites ----------


var s = new Array();

s[0] = "JSE^form.html^Enter JSE in the example above ...^open source, free, client side";
s[1] = "JSE DOCUMENTATION^doc.txt^JSE is a client-side local site search engine. Written entirely in JavaScript 1.3 it works with browsers from Netscape 4.7 to Microsoft Internet Explorer 6.0 ...^ faq, docs, instructions";


// ---------- end of script properties and sites ----------


var cookies = document.cookie;
var p = cookies.indexOf("d=");

if (p != -1) {
      var st = p + 2;
      var en = cookies.indexOf(";", st);
      if (en == -1) {
            en = cookies.length;
      }
      var d = cookies.substring(st, en);
      d = unescape(d);
}
var od = d;
var m = 0;
if (d.charAt(0) == '"' && d.charAt(d.length - 1) == '"') {
      m = 1;
}

var r = new Array();
var co = 0;

if (m == 0) {
      var woin = new Array();
      var w = d.split(" ");
      for (var a = 0; a < w.length; a++) {
            woin[a] = 0;
            if (w[a].charAt(0) == '-') {
                  woin[a] = 1;
            }
      }
      for (var a = 0; a < w.length; a++) {
            w[a] = w[a].replace(/^\-|^\+/gi, "");
      }
      a = 0;
      for (var c = 0; c < s.length; c++) {
            pa = 0;
            nh = 0;
            for (var i = 0; i < woin.length; i++) {
                  if (woin[i] == 0) {
                        nh++;
                        var pat = new RegExp(w[i], "i");
                        var rn = s[c].search(pat);
                        if (rn >= 0) {
                              pa++;
                        } else {
                              pa = 0;
                        }
                  }
                  if (woin[i] == 1) {
                        var pat = new RegExp(w[i], "i");
                        var rn = s[c].search(pat);
                        if (rn >= 0) {
                              pa = 0;
                        }
                  }
            }
            if (pa == nh) {
                  r[a] = s[c];
                  a++;
            }
      }
      co = a;
}

if (m == 1) {
      d = d.replace(/"/gi, "");
      var a = 0;
      var pat = new RegExp(d, "i");
      for (var c = 0; c < s.length; c++) {
            var rn = s[c].search(pat);
            if (rn >= 0) {
                  r[a] = s[c];
                  a++;
            }
      }
      co = a;

}


function return_query() {
      document.jse_Form.d.value = od;
}

function num_jse() {
      document.write(co);
}

function out_jse() {
      if (co == 0) {
            document.write('Your search did not match any documents.<p>Make sure all keywords are spelt correctly.<br>Try different or more general keywords.');
            return;
      }
      for (var a = 0; a < r.length; a++) {
            var os = r[a].split("^");
            if (bold == 1 && m == 1) {
                  var br = "<b>" + d + "</b>";
                  os[2] = os[2].replace(pat, br);
            }
            if (include_num == 1) {
                  document.write(a + 1, '. <a href="', os[1], '">', os[0], '</a><br>', os[2], '<p>');
            } else {
                  document.write('<a href="', os[1], '">', os[0], '</a><br>', os[2], '<p>');
            }
      }

}

results.html:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">

<html>
<head>
<title>JSE SEARCH RESULTS</title>

<script language="JavaScript1.3" type="text/javascript" src="jse_form.js"></script>
<script language="JavaScript1.3" type="text/javascript" src="jse_search.js"></script>

</head>
<body>


<!-- Find -->


<form name="jse_Form" onsubmit="search_form(jse_Form);return false">
<input type="text" name="d">
<input type="button" value="Go" onClick="search_form(jse_Form)">
</form>
<script language="JavaScript1.3" type="text/javascript">return_query()</script>


<!-- Mid -->

<script language="JavaScript1.3" type="text/javascript">num_jse()</script> page(s) found
<hr>
<p>
<script language="JavaScript1.3" type="text/javascript">
out_jse()
</script>
<p>&nbsp;



</body>
</html>

DOCUMENTATION:
JSE DOCUMENTATION

JSE Version 1.0
--------------
*  Addendum*
*  Modified by JavaScriptKit.com (Nov 26, '02) with the following:
** Added onSubmit event handlers inside FORM to handle "enter" key
** Modified formatting of form/result pages to be NS4 friendly.
** http://www.javascriptkit.com/script/script2/jse/
-------------

CONTENTS

1. A description of JSE and how the scripts work
2. The HTML
3. The script properties
4. Adding the site data
5. Licence


1. A description of JSE and how the scripts work

JSE is a client-side site search engine. Written entirely in
JavaScript 1.3 it works with browsers from Netscape 4.7 to
Microsoft Internet Explorer 6.0.

JSE is Open Source and is released under the GPL licence.

It's best suited to smaller sites with less than 50 pages. JSE is
platform independent and doesn't require .NET, ASP, CGI or any
other technologies on your host.

JSE circumvents HMTL's inability to pass a value from one page to
another by using a session or non-persistent cookie. The cookie
expires when the user's session ends.

JSE consists of two scripts. The first writes a cookie containing
the search words and then loads the results page. This script is
very short, only a few lines, as you may want your search box on
more than one page. The second script does all the work; reading
the cookie, defining the matches and generating the search results.

For users, searching is similar to Google. A preceding minus
character excludes a word, while phrases are supported within
double-quotes.


2. The HTML

The HEAD section of the page containing the search box should
include:

<script language="JavaScript1.3" type="text/javascript"
src="jse_form.js"></script>

The FORM code should be similar to the example below. Please note
that the FORM NAME and initial INPUT values are fixed, and that
the form must call the search_form(jse_Form) JavaScript function.

<form name="jse_Form">
<input type="text" name="d">
<input type="button" value="Go" onclick="search_form(jse_Form)">
</form>

For the results page, the HEAD section should include:

<script language="JavaScript1.3" type="text/javascript"
src="jse_search.js"></script>

The code below returns the number of matches found:

<script language="JavaScript1.3" type="text/javascript">num_jse()
</script>

The code below returns the search results:

<script language="JavaScript1.3" type="text/javascript">out_jse()
</script>

There is one additional, nonessential function. This should only
be used on the results page, and should follow the FORM code. It
adds the previous search query to the FORM search field.

<script language="JavaScript1.3" type="text/javascript">
return_query()</script>


3. The script properties

There are several script properties to control output and specify
file location. Each of these properties can be found at the
beginning of the scripts.

There is one property for the jse_form.js script:

results_location = PAGE LOCATION

Where PAGE LOCATION is the location of the search results
page. This property also accepts a full URL address.

Example:
var results_location = "results.html";

There are two properties for the jse_search.js script:

include_num = VALUE

Where VALUE is either 1 or 0. Sets results numbering on
or off.

Example:
var include_num = 1;

bold = VALUE

Search terms are highlighted in bold if VALUE is 1, and
returned as plain text if VALUE is 0.

Example:
var bold = 0;


4. Adding the site data

The site data is held as a JavaScript array in the jse_search.js
script, below the script variables. The format of the array is:

s[REC NUM] = "TITLE^URL OR FILENAME^DESCRIPTIVE LINE^KEYWORDS";

Where ^ is the field separator or delimiter. REC NUM is a
sequential integer number, starting at zero. URL OR FILENAME
is the HTML filename or full URL for the page. TITLE and
DESCRIPTIVE LINE are self-explanatory, while KEYWORDS is also
searched for matches. The following is a short example:

s[0] = "JSE^jse.html^JSE is a local site search engine ...^open
source, free";

A search for "search engine" or "open source" would return:

1. JSE
JSE is a local site search engine ...


5. Licence

JSE is Open Source and is released under the GPL licence. A copy
of the GNU General Public License is included along with this
documentation.

JSE 1.0
This or the latest version of JSE can be downloaded from
www.tipue.com/products/jse

jse@tipue.com

JSE Copyright (C) 2002 Tri-State Consultants

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

Hope that helps,
Benjamin
ASKER CERTIFIED SOLUTION
Avatar of superm401
superm401

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of superm401
superm401

The above will only include pages that Google indexes, but that shouldn't be a problem if your pages have been up for a while.
Have google do it for you, completely automated.  Look into this, it is well worth reading about --

http://www.google.co.uk/searchcode.html
The google code is useless unless all your pages are indexed by it...
Avatar of Colly321

ASKER

Hi guys

Thanks for all the advice.

I think the Google idea is a good one, but I am pushing for the client to use a database which is obviously searchable.

If they insist on not having one (probably due to costs) then I will go with Google.

Cheers

Andy
I think you missed the point -- google, in the link I gave, DOES do it on your own database.
First of all, sciwriter, when you say that "Google[...]DOES do it on your own database," that is incorrect.  When a search is performed using that feature, Google uses its own database, which includes most public web sites.  Second of all, if Google used the site's database, that would be inadequate because this site has no database.