?
Solved

Search Facility

Posted on 2005-03-02
8
Medium Priority
?
437 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Colly321
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:benwiggy
ID: 13441238
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
0
 
LVL 6

Accepted Solution

by:
superm401 earned 2000 total points
ID: 13455632
While impressive, that search app is inherently limited by its own descriptors.  Unless I have grossly misinterpreted something, it appears to only search the array that is provided in the .js file.  This is useful to some extent, but you must remember, this will not find any text that is not in the array.  Instead of using this, I recommend that you use Google, which offers free site search:

<!-- SiteSearch Google -->
<FORM method=GET action="http://www.google.com/search">
<input type=hidden name=ie value=UTF-8>
<input type=hidden name=oe value=UTF-8>
<TABLE bgcolor="#FFFFFF"><tr><td>
<A HREF="http://www.google.com/">
<IMG SRC="http://www.google.com/logos/Logo_40wht.gif"
border="0" ALT="Google"></A>
</td>
<td>
<INPUT TYPE=text name=q size=31 maxlength=255 value="">
<INPUT type=submit name=btnG VALUE="Google Search">
<font size=-1>
<input type=hidden name=domains value="YOUR DOMAIN NAME"><br><input type=radio name=sitesearch value=""> WWW <input type=radio name=sitesearch value="YOUR DOMAIN NAME" checked> YOUR DOMAIN NAME <br>
</font>
</td></tr></TABLE>
</FORM>
<!-- SiteSearch Google -->

That will put a Google search box on your web site, which will allow web searches as well as searches of only your own domain.  If you want to get even fancier, you can try:

http://www.google.com/services/free.html

It lets you customize the results page with a logo(if you have one) and colors of your choice.

Considering that you can't do your own server-side processing, an off-site solution is your only good choice.  If you have questions, post.
0
 
LVL 6

Expert Comment

by:superm401
ID: 13455638
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:sciwriter
ID: 13456336
Have google do it for you, completely automated.  Look into this, it is well worth reading about --

http://www.google.co.uk/searchcode.html
0
 
LVL 6

Expert Comment

by:benwiggy
ID: 13456758
The google code is useless unless all your pages are indexed by it...
0
 

Author Comment

by:Colly321
ID: 13456967
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
0
 
LVL 23

Expert Comment

by:sciwriter
ID: 13460274
I think you missed the point -- google, in the link I gave, DOES do it on your own database.
0
 
LVL 6

Expert Comment

by:superm401
ID: 13468635
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.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question