drdebate
10-18-2003, 02:20 AM
I have the script below to search any one search engine and display the results in a new window. At present, when the form is submitted the document is refreshed in addition to the new window opening. Is it possible to adjust this script so that it does not refresh when submit is clicked?
<SCRIPT>
<!-- hide this script from non-JavaScript browsers
var MAX_ENGINES = 75;
var SNARK_STRING = "hunting+the+snark";
function MakeArray(n) {
for (var i = 1; i <= n; i++) {
this[i] = 0;}
this.maxlen = n;
this.len = 0;
return this;}
var engs = new MakeArray(MAX_ENGINES);
function find_substring(needle, haystack) {
var i, needlen = needle.length, haylen = haystack.length;
for (i=0; i<=haylen-needlen; i++) {
if (needle == haystack.substring(i,i+needlen))
return i;}
return false;}
function Engine(name, opts, home, search) {
var snark = find_substring(SNARK_STRING, search);
this.name = name;
this.opts = opts;
this.home = home;
this.pre_snark = search.substring(0,snark);
this.post_snark= search.substring(snark+SNARK_STRING.length, search.length);}
function Add(name, opts, home, search) {
engs.len++;
if (engs.len <= engs.maxlen) {
engs[engs.len] = new Engine(name, opts, home, search)}
else {
alert("Better increase MAX_ENGINES: " + engs.len + ">" + engs.maxlen)}}
Add("Google", "SELECTED",
"http://www.google.com",
"http://www.google.com/search?q=hunting+the+snark");
Add("MSN", "",
"http://www.msn.com",
"http://search.msn.com/results.asp?RS=CHECKED&FORM=MSNH&v=1&q=hunting+the+snark")
Add("Yahoo", "",
"http://www.yahoo.com",
"http://search.yahoo.com/search?p=hunting+the+snark&sub=Search&ei=UTF-8&fr=fp-top");
Add("Lycos", "",
"http://www.lycos.com/",
"http://search.lycos.com/main/default.asp?lpv=1&loc=searchhp&query=hunting+the+snark" );
Add("Altavista", "",
"http://www.altavista.com/",
"http://www.altavista.com/sites/search/web?q=hunting+the+snark" );
Add("Metacrawler", "",
"http://www.metacrawler.com/",
"http://search.metacrawler.com/texis/search?brand=metacrawler&q=hunting+the+snark" );
Add("Ask Jeeves", "",
"http://www.ask.com/",
"http://www.ask.com/main/askjeeves.asp?ask=hunting+the+snark" );
function HandleForm(form) {
form.submit();
var i, oldq=form.query.value, newq="";
for (i=0; i<oldq.length; i++) { // compress [ ]+ into \+
var thischar = oldq.charAt(i);
if (thischar != ' ')
newq += thischar;
else if (lastchar != ' ')
newq += '+';
lastchar = thischar;}
var eng = engs[1+form.service.selectedIndex];
window.open(newq ? eng.pre_snark + newq + eng.post_snark : eng.home);}
function DisplayForm() {
document.writeln('<FORM OnSubmit="HandleForm(this)">Search the web with ');
document.writeln('<SELECT name="service">');
for (i=1; i <= engs.len; i++) {document.writeln("<OPTION " + engs[i].opts + "> " + engs[i].name);}
document.writeln('</SELECT> for <INPUT size=25 name="query"> ');
document.writeln('<input type=submit value=" Search">');
document.writeln('<input type=reset value=" Reset">');
document.writeln('</FORM>');}
DisplayForm();
</SCRIPT>
<SCRIPT>
<!-- hide this script from non-JavaScript browsers
var MAX_ENGINES = 75;
var SNARK_STRING = "hunting+the+snark";
function MakeArray(n) {
for (var i = 1; i <= n; i++) {
this[i] = 0;}
this.maxlen = n;
this.len = 0;
return this;}
var engs = new MakeArray(MAX_ENGINES);
function find_substring(needle, haystack) {
var i, needlen = needle.length, haylen = haystack.length;
for (i=0; i<=haylen-needlen; i++) {
if (needle == haystack.substring(i,i+needlen))
return i;}
return false;}
function Engine(name, opts, home, search) {
var snark = find_substring(SNARK_STRING, search);
this.name = name;
this.opts = opts;
this.home = home;
this.pre_snark = search.substring(0,snark);
this.post_snark= search.substring(snark+SNARK_STRING.length, search.length);}
function Add(name, opts, home, search) {
engs.len++;
if (engs.len <= engs.maxlen) {
engs[engs.len] = new Engine(name, opts, home, search)}
else {
alert("Better increase MAX_ENGINES: " + engs.len + ">" + engs.maxlen)}}
Add("Google", "SELECTED",
"http://www.google.com",
"http://www.google.com/search?q=hunting+the+snark");
Add("MSN", "",
"http://www.msn.com",
"http://search.msn.com/results.asp?RS=CHECKED&FORM=MSNH&v=1&q=hunting+the+snark")
Add("Yahoo", "",
"http://www.yahoo.com",
"http://search.yahoo.com/search?p=hunting+the+snark&sub=Search&ei=UTF-8&fr=fp-top");
Add("Lycos", "",
"http://www.lycos.com/",
"http://search.lycos.com/main/default.asp?lpv=1&loc=searchhp&query=hunting+the+snark" );
Add("Altavista", "",
"http://www.altavista.com/",
"http://www.altavista.com/sites/search/web?q=hunting+the+snark" );
Add("Metacrawler", "",
"http://www.metacrawler.com/",
"http://search.metacrawler.com/texis/search?brand=metacrawler&q=hunting+the+snark" );
Add("Ask Jeeves", "",
"http://www.ask.com/",
"http://www.ask.com/main/askjeeves.asp?ask=hunting+the+snark" );
function HandleForm(form) {
form.submit();
var i, oldq=form.query.value, newq="";
for (i=0; i<oldq.length; i++) { // compress [ ]+ into \+
var thischar = oldq.charAt(i);
if (thischar != ' ')
newq += thischar;
else if (lastchar != ' ')
newq += '+';
lastchar = thischar;}
var eng = engs[1+form.service.selectedIndex];
window.open(newq ? eng.pre_snark + newq + eng.post_snark : eng.home);}
function DisplayForm() {
document.writeln('<FORM OnSubmit="HandleForm(this)">Search the web with ');
document.writeln('<SELECT name="service">');
for (i=1; i <= engs.len; i++) {document.writeln("<OPTION " + engs[i].opts + "> " + engs[i].name);}
document.writeln('</SELECT> for <INPUT size=25 name="query"> ');
document.writeln('<input type=submit value=" Search">');
document.writeln('<input type=reset value=" Reset">');
document.writeln('</FORM>');}
DisplayForm();
</SCRIPT>