ljl 发表于 2007-4-27 10:09:07

***测试搜索***不错的页面搜索脚本

把下列代码加到网页的<body></body>标签之间 从google下载我的照片管理软件
要完成此效果需要两个步骤

第一步:把如下代码加入到<body>区域中

<div align="center">
<script language="javascript">

<!--

// 下面是负责搜索的页面的全路径或相对路径

var search_htm_url = "test.htm";



function searchpage() {

if ((document.searchpage.keyword.value.length == 0)

|| (document.searchpage.keyword.value == " ")) {

alert("请填入关键字!");

} else {

sel = document.searchpage.and_or.selectedindex;

location.href = search_htm_url + "?"

+ escape(document.searchpage.keyword.value)

+ (sel==0?"&and":(sel==2?"&exact":"&or"));

}

return false;

}



document.write('<form name="searchpage" onsubmit="return searchpage()">'

+'请填入关键字(试试填入java): <input type="text" size=22 name="keyword"> '

+'<input type="button" value="search" onclick="searchpage()"><br>'

+'<select name="and_or" size=1><option>关键字与搜索<option>关键字或搜索 '

+'<option>关键字异或搜索</select></form>');

// -->

</script>
</div>


第二步:同一目录新建“test.htm”内容如下

<form name="database">
<input type="hidden" name="list"
value="http://typhoon.500.to~typhoon start|javascript乐园^javascript fairyland
">
</form>
<!--以上可以自行调整-->
<script language="javascript">
<!--
function page(url,title,keywords,description) {
while ((url.length > 0) && (url.charat(0) == " ")) {
url = url.substring(1,url.length);
}
this.url = url;
while ((title.length > 0) && (title.charat(0) == " ")) {
title = title.substring(1,title.length);
}
this.title = title;
this.keywords = keywords;
this.description = description;
return this;
}
function database() {
var pos = 0;
while ((pos1 = amorphous.indexof("~",pos)) != -1) {
pos2 = amorphous.indexof("|",pos1+1);
pos3 = amorphous.indexof("^",pos2+1);
pos4 = amorphous.indexof("*",pos3+1);
if ((pos2 != -1)
&& (pos2 < pos3) && (pos3 < pos4)
&& (pos4 <= amorphous.indexof("*",pos))) {
this = new page(amorphous.substring(pos,pos1),
amorphous.substring(pos1+1,pos2),
amorphous.substring(pos2+1,pos3),
amorphous.substring(pos3+1,pos4));
pos = pos4+1;
} else { // error reading amorphous database
if (pos+30 <= amorphous.length)
alert('error reading in amorphous database around "'
+ amorphous.substring(pos,pos+30) + '"');
pos = amorphous.indexof("*",pos) + 1;
}
}
return this;
}
function search(str) {
menu_length = 0;
temp = new object();
temp_length = 0;
words_length = 0;
words = new object();
pos = 0;
while ((pos = str.indexof(" ")) != -1
&& and_search != "exact") {
words = str.substring(0,pos);
if (words.length > 0)
words_length++;
if (str.length == 1)
str="";
else
str = str.substring(pos+1,str.length);
}
if (str.length > 0)
words = str;
for (q=0;q<words_length;q++) {
temp_length = 0;
str = words.tolowercase();
len = (and_search=="and"&&q>0?menu_length:database_length);
for (n=0; n<len; n++) {
if (and_search=="and"&&q>0) {
combo = (menu.title + " " + menu.description
+ " " + menu.keywords).tolowercase();
} else {
combo = (database.title + " " + database.description
+ " " + database.keywords).tolowercase();
}
if (combo.indexof(str) != -1) // found
temp = (and_search=="and"&&q>0?menu:database);
}
if (and_search!="and" && q>0) {
added = 0;
for (i=0;i<temp_length;i++) {
duplicate = false;
for (j=0;j<menu_length&&!duplicate;j++) {
if (menu == temp) {
duplicate = true;
}
}
if (!duplicate)
menu = temp;
}
menu_length += added;
} else {
for(h=0;h<temp_length;h++)
menu = temp;
menu_length = temp_length;
}
}
}
function entry() {
if ((document.entryform.keyword.value.length == 0)
|| (document.entryform.keyword.value == " ")) {
alert("你必须填写关键字!");
return false;
}
and_search = (document.entryform.and_or.selectedindex == 0?"and":"or");
if (document.entryform.and_or.selectedindex == 2)
and_search = "exact";
location.href = location.pathname + "?"
+ escape(document.entryform.keyword.value)
+ (and_search != "or"?"&"+and_search:"");
return false;
}
function redword(str) {
for(r=0; r<words_length; r++) {
pos = -3;
word = words.tolowercase();
while ((pos = str.tolowercase().indexof(word,pos+3)) != -1) {
val = pos+word.length;
str = str.substring(0,pos) + "*"
+ str.substring(pos,val) + "|"
+ str.substring(val,str.length);
}
}
pos = -16;
while ((pos = str.tolowercase().indexof("*",pos+16)) != -1)
str = str.substring(0,pos) + "<font color=red>"
+ str.substring(pos+1,str.length);
pos = -7;
while ((pos = str.tolowercase().indexof("|",pos+7)) != -1)
str = str.substring(0,pos) + "</font>"
+ str.substring(pos+1,str.length);
return str;
}

var amorphous = document.database.list.value;
temp_str = amorphous.substring(amorphous.length-2,amorphous.length);
if (temp_str.indexof("*") == -1)
amorphous += "* ";
else
amorphous += " "; // amorphous database must have characters after last asterisk

database_length = 0; // netscape 2 fix
var database = new database(); // read in from amorphous database

menu_length = 0; // netscape 2 fix
var menu = new object();

string = "";
and_search = "or";
if (location.search.length > 1) {
string = unescape(location.search.substring(1,location.search.length));
pos = 0;
while ((pos = string.indexof('"',pos)) != -1) {
string = string.substring(0,pos) + '\\"' + string.substring(pos+1,string.length);
pos += 2;
}
if (string.substring(string.length-4,string.length) == "&and") {
string = string.substring(0,string.length-4);
and_search = "and";
} else if (string.substring(string.length-6,string.length) == "&exact") {
string = string.substring(0,string.length-6);
and_search = "exact";
} else if (string.substring(string.length-3,string.length) == "&or") {
string = string.substring(0,string.length-3);
and_search = "or";
}
search(string);
}
document.write('<center><form name="entryform" onsubmit="return entry()">'
+'请填入关键字:<input type="text" size=22 '
+'name="keyword" value="'+string+'"> '
+'<input type="button" value="search" onclick="entry()" style="background-color: #8000ff; color: rgb(255,255,255)"><select name="and_or" '
+'size=1><option'+(and_search=="and"?" selected":"")+'>关键字搜索 '
+'(与)<option'+(and_search=="or"?" selected":"")+'>关键字搜索 '
+'(或)<option'+(and_search=="exact"?" selected":"")+'>关键字搜索'
+'(异或)</select></form></center><br>');
if (location.search.length > 1)
document.write('查询结果:<br><br>\n');

for (n=0; n<menu_length; n++)
document.write('<a href="'+menu.url+'">'+menu.title

+'</a><br>'+redword(menu.description)+'<br>关键字: '
+redword(menu.keywords)+'<br><br>\n');
if ((menu_length == 0) && (location.search.length > 1))
document.write('对不起:你查询的关键字"'+string+'"没有发现!\n');
// -->
</script>


      










--------------------------------------

拥有三人行论坛管理系列软件,一个人就可以管理一个论坛的更新维护。还能使您的论坛有千人在线的真实火暴效果,让您不在为论坛因没有人气而留不住会员而烦恼,使您的论坛早日成为一流的论坛。详情请登陆网站:http://www.cnsrx.cn 三人行论坛管理软件,站长必备的专业好工具。(您使用的是免费版本,购买收费版本后,将不带广告。业务QQ:247216 44985227)
页: [1]
查看完整版本: ***测试搜索***不错的页面搜索脚本