JavaScript实例 站内搜索例子 (一)
时间:2007-02-17 来源:PHP爱好者
脚本说明:
第一步:把如下代码加入<body>区域中
<script language="JavaScript">
<!--
// 下面是负责搜索的页面的全路径或相对路径
var search_htm_url = "searchsource.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()">'
+'请填入关键字: <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>
第二步:新建searchsource.htm 其内容为
<!-- 代码开始-->
<form NAME="database">
<input type="hidden" name="list"
value="http://www.njcatv.net~南京有线电视台网站|njcatv 南京有线电视台 网页特效 影音空间 新闻 javascript
java applet 健康之桥^为广大南京市民提供全方位的信息和帮助*http://www.sina.com.cn~最全的中文新闻发部站点
|sina 新浪 新闻 super 王志东 聊天室^国内最大的新闻网站!*http://www.163.net~最大的综合中文网站|163 netease
网易 丁磊 新闻 yeah 126 娱乐 freemail 聊天室^最大的综合中文网站,为你提供全方位的各种信息,是你上网的好去
处!*http://www.sohu.com~最大的中文引擎搜索站点|sohu search 搜索 引擎 张朝阳 聊天室^最大的中文引擎搜索站点,
是你上网查找资料的最好去处!*http://www.sohu.com~最大的中文引擎搜索站点|sohu search 搜索 引擎 张朝阳 聊天室^
最大的中文引擎搜索站点,是你上网查找资料的最好去处!*http://www.sohu.com~最大的中文引擎搜索站点|sohu search
搜索 引擎 张朝阳 聊天室^最大的中文引擎搜索站点,是你上网查找资料的最好去处!*http://www.sohu.com~最大的中文
引擎搜索站点|sohu search 搜索 引擎 张朝阳 聊天室^最大的中文引擎搜索站点,是你上网查找资料的最好去处!
*http://www.sohu.com~最大的中文引擎搜索站点|sohu search 搜索 引擎 张朝阳 聊天室^最大的中文引擎搜索站点,是
你上网查找资料的最好去处!*http://www.sohu.com~最大的中文引擎搜索站点|sohu search 搜索 引擎 张朝阳 聊天室^最
大的中文引擎搜索站点,是你上网查找资料的最好去处!">
</form>
脚本说明:
把如下代码加入<body>区域中
<script language="javascript">
var key = "";
function makeEntry (){
this.Date = "";
this.Name="";
this.URL = "";
this.Desc = "";
this.Category = "";
return this;
}
function makeArray(n) {
this.length = n;
for (var k = 1; k <= n; k++) {
this[k] = "";
}
return this;
}
function makeLinks(size) {
this.length = size;
for (var r=1; r<= size; r++) {
this[r] = new makeEntry();
this[r].Date = datesArray[r];
this[r].Name = namesArray[r];
this[r].URL = urlsArray[r];
this[r].Desc = descArray[r];
}
return this;
}
var linksize=0
datesArray = new makeArray(linksize);
namesArray = new makeArray(linksize);
urlsArray = new makeArray(linksize);
descArray = new makeArray(linksize);
var arraycount=0
arraycount += 1
datesArray[arraycount] = "5/30/2000"
urlsArray[arraycount] = "http://www.163.net"
namesArray[arraycount] = "网易"
descArray[arraycount] = "163 netease yeah 126 chinese "
//alert(arraycount)
arraycount += 1
datesArray[arraycount] = "5/30/2000 "
urlsArray[arraycount] = "http://www.sina.com.cn"
namesArray[arraycount] = "新浪网"
descArray[arraycount] = "sina net chat chatting news super"
//alert(arraycount)
arraycount += 1
datesArray[arraycount] = "5/30/2000 "
urlsArray[arraycount] = "http://www.sohu.com"
namesArray[arraycount] = "搜狐"
descArray[arraycount] = "sohu net chat news "
//alert(arraycount)
linksize = arraycount;
// ----end data -------
function showAll(linkobj) {
for (var s=1; s<= linkobj.length; s++) {
showLink(linkobj,s);
}
}
function showLink (links, index) {
//document.write("<table border>");
document.write("<tr><td>" + links[index].Date +"</td>");
document.write("<td><a href=" + links[index].URL +">" + links[index].Name + "</a></td>");
document.write("<td>" + links[index].Desc + "</td></tr>");
//document.write("</table>");
}
function searchLinks(links, keyword){
document.write("搜寻结果: " +keyword +"<br>");
document.write("<table border>");
for (var q=1; q<=links.length; q++) {
//document.write(q+".")
if (links[q].URL.indexOf(keyword) != -1){
// document.write("Search Results for keyword: "+keyword+ "<br>" +links[q].Name +"<p>")
//document.write("搜寻结果: "+keyword+ "<br>" );
showLink(links,q);
continue;
}
if (links[q].Desc.indexOf(keyword) != -1) {
showLink(links,q);
continue;
}
if (links[q].Date.indexOf(keyword) != -1) {
showLink(links,q);
continue;
}
if (links[q].Name.indexOf(keyword) != -1) {
showLink(links,q);
continue;
}
}
document.write("</table>");
}
// final stuff
// the main program ---
jsi = new makeLinks(linksize);
document.write("<title>站内查询</title>");
searchLinks(jsi, prompt("输入查询关键字:","输入查询关键字:"));
document.write("<hr>");
document.write("按<b>search again</b>再次查询<hr>");
document.write("<form><input type=button onClick='history.go(0)' value='Search Again'></form>");
// show all the links
//document.write("<table border>");
//showAll(jsi);
//document.write("</table>");
</script>
脚本说明:第一步:把如下代码加入<body>区域中
<script language="javascript">
var key = "";
function makeEntry (){
this.Name="";
this.URL = "";
this.Desc = "";
this.Category = "";
this.Target = "";
return this;
}
function makeArray(n) {
this.length = n;
for (var k = 1; k <= n; k++) {
this[k] = "";
}
return this;
}
function makeLinks(size) {
this.length = size;
for (var r=1; r<= size; r++) {
this[r] = new makeEntry();
this[r].Name = namesArray[r];
this[r].URL = urlsArray[r];
this[r].Desc = descArray[r];
this[r].Category = categoryArray[r];
this[r].Target = targetArray[r];
}
return this;
}
var linksize=0
datesArray = new makeArray(linksize);
namesArray = new makeArray(linksize);
urlsArray = new makeArray(linksize);
descArray = new makeArray(linksize);
categoryArray = new makeArray(linksize);
targetArray = new makeArray(linksize);
var arraycount=0
<!-- 数据库部分由此开始 -->
arraycount += 1
urlsArray[arraycount] = "http://java2000.126.com"
namesArray[arraycount] = "java脚本资源站Chinese-GB"
descArray[arraycount] = "java, javascript, scripts, applet, html, dhtml ,activex, midi, search, downloads,
脚本, free, 免费资源"
categoryArray[arraycount] = "最酷的java脚本资源站,大量详细的java源代码和实例说明!"
targetArray[arraycount] = "target=_blank"
arraycount += 1
urlsArray[arraycount] = "http://members.aol.com/MASTER54X/Anime.html"
namesArray[arraycount] = "Anime Master"
descArray[arraycount] = "anime master, anime, project a-ko, vampire hunter D, tenchi muyo, bubblegum
crisis, sailor moon, sailormoon, gunsmith cats, slayers, all purpose cultural cat girl nuku nuku, plastic
little, devil hunter yohko, iria: zeiram the animation, iria, ranma 1/2, ranma, fist of the north star,
multimedia, galleries, pic galleries, picture gallery, image gallery, image galleries"
categoryArray[arraycount] = "Multimedia, Image galleries for project a-ko, vampire hunter D, tenchi muyo,
bubblegum crisis, sailormoon, gunsmith cats, slayers, all purpose cultural cat girl nuku nuku, plastic
little, devil hunter yohko, iria: zeiram the animation, ranma 1/2, and fist of the north star."
targetArray[arraycount] = "target=_blank"
arraycount += 1
urlsArray[arraycount] = "http://www.geocities.com/Tokyo/Towers/6508/"
namesArray[arraycount] = "The Anime Shrine"
descArray[arraycount] = "the anime shrine, anime shrine, anime, the anime shrine, anime shrine, manga,
saintseiya, fushigiyuugi, fushigi yuugi, evangelion, neon genesis evangelion, eva, ranma, ranma 1/2"
categoryArray[arraycount] = "this is a fairly large site with large image galleries from many dif animes,
many links, gives out awards, has 3 voting shrines, also sub-Shrines for Fushigi Yuugi, SaintSeiya,
Evangelion, and Ranma 1/2. And much more! Must see!"
targetArray[arraycount] = "target=_blank"
arraycount += 1
urlsArray[arraycount] = "http://home.taegu.net/~virus/"
namesArray[arraycount] = "Evangelion Site"
descArray[arraycount] = "evangelion site, anime, evangelion, neon genesis, neon genesis evangelion, eva,
neon, genesis"
categoryArray[arraycount] = "This site is about evangelion, but available in Korean only..."
targetArray[arraycount] = "target=_blank"
arraycount += 1
urlsArray[arraycount] = "http://animeonline.org/"
namesArray[arraycount] = "The Animeonline Network"
descArray[arraycount] = "the animeonline network, anime, animecca, news, information, anime magazine,
magazine, zine, e-zine"
categoryArray[arraycount] = "Your Online Anime Magazine!"
targetArray[arraycount] = "target=_blank"
arraycount += 1
urlsArray[arraycount] = "http://www.visi.com/~vdgaijin/gaijin.html"
namesArray[arraycount] = "Psychommu Gaijin Anime E-zine"
descArray[arraycount] = "psychommu gaijin anime e-zine, anime, music, links, news, information, e-zine,
zine"
categoryArray[arraycount] = "Our aim is to provide you with news and information on some of the coolest
aspects of Anime. Old and new! So come on in and enjoy!"
targetArray[arraycount] = "target=_blank"
arraycount += 1
urlsArray[arraycount] = "http://the.animearchive.org/"
namesArray[arraycount] = "Ultimate Animanga Archive"
descArray[arraycount] = "ultimate animanga archive, anime, manga, images, pictures, image gallery, midi,
image galleries, music, chat, information, descriptions, info"
categoryArray[arraycount] = "Searchable page of images, info, and descriptions of anime OAVs and series."
targetArray[arraycount] = "target=_blank"
arraycount += 1
urlsArray[arraycount] = "http://otakuworld.com/"
namesArray[arraycount] = "Otaku World"
descArray[arraycount] = "otaku world, anime, manga, desktop themes, anime themes, search engine, link
database, web rings, web ring"
categoryArray[arraycount] = "One of the largest multimedia anime sources on the web!"
targetArray[arraycount] = "target=_blank"
arraycount += 1
urlsArray[arraycount] = "http://www.usagi.com"
namesArray[arraycount] = "Usagi.com"
descArray[arraycount] = "usagi.com, anime, manga, links, link, database, tenchi, tenchi muyo, download,
multimedia"
categoryArray[arraycount] = "Your guide to the online anime community!"
targetArray[arraycount] = "target=_blank"
arraycount += 1
urlsArray[arraycount] = "http://www.si.hhs.nl/~v962343/home.html"
namesArray[arraycount] = "Richie's Place"
descArray[arraycount] = "richie's place, anime, manga, pictures, pics, movies, links, gif animations,
animation, hentai, java, link"
categoryArray[arraycount] = "Richie's Place! Over 30 pages! with a picture gallery, movies, gif
animations, games, java and more."
targetArray[arraycount] = "target=_blank"
linksize = arraycount;
// ----数据库结束 ------
// ----以下是输出结果检索结果的页面代码 ----
function searchLinks(links, keyword){
document.write("<BODY fontsize='2' BGCOLOR='#000000' TEXT='#ffffff' LINK='#ccffcc'
VLINK='#ffffff'>")
document.write("搜索关键字结果: <strong>" +keyword +"</strong><br><br>");
for (var q=1; q<=links.length; q++) {
if (links[q].URL.toLowerCase().indexOf(keyword) != -1){
document.write("<a href=" + links[q].URL +" " + links[q].Target + ">" + links[q].Name + "</a> -
");
document.write( links[q].Category + "<br><br>");
continue;
}
if (links[q].Desc.toLowerCase().indexOf(keyword) != -1) {
document.write("<a href=" + links[q].URL +" " + links[q].Target + ">" + links[q].Name + "</a> -
");
document.write( links[q].Category + "<br><br>");
continue;
}
if (links[q].Name.toLowerCase().indexOf(keyword) != -1) {
document.write("<a href=" + links[q].URL +" " + links[q].Target + ">" + links[q].Name + "</a> -
");
document.write( links[q].Category + "<br><br>");
continue;
}
}
}
</script>
<font size="5" color="#FF0000">站内搜索引擎<br>
</font><font color="#FF0000"> <font color="#000000">测试:输入关键字"java"
或中文字符"免费" 查询。</font></font> </div>
<p align=Center>
<form name="form">
<div align="center">
<p>查询关键字:
<input type="text" size=20 name="search">
<input type="button" name="submit" value="开始查询" onClick="validate_form()" class="pt9">
<input type="reset" name="reset" value="清除" class="pt9">
<br>
</p>
</div>
</form>
<!-- 以下是合法性检测 -->
<script>
function validate_form() {
txt = document.form.search.value;
if (txt.indexOf(".") != -1){
alert("Search is invalid! Contains a '.'"); return;}
if (txt.indexOf(",") != -1){
alert("Search is invalid! Contains a ','"); return;}
if (txt.indexOf("@") != -1){
alert("Search is invalid! Contains a '@''"); return;}
if (txt.indexOf("!") != -1){
alert("Search is invalid! Contains a '!''"); return;}
if (txt.indexOf("#") != -1){
alert("Search is invalid! Contains a '#''"); return;}
if (txt.indexOf("$") != -1){
alert("Search is invalid! Contains a '$''"); return;}
if (txt.indexOf("%") != -1){
alert("Search is invalid! Contains a '%''"); return;}
if (txt.indexOf("^") != -1){
alert("Search is invalid! Contains a '^''"); return;}
if (txt.indexOf("&") != -1){
alert("Search is invalid! Contains a '&''"); return;}
if (txt.indexOf("*") != -1){
alert("Search is invalid! Contains a '*''"); return;}
if (txt.indexOf("(") != -1){
alert("Search is invalid! Contains a '(''"); return;}
if (txt.indexOf(")") != -1){
alert("Search is invalid! Contains a ')''"); return;}
if (txt.indexOf("[") != -1){
alert("Search is invalid! Contains a '[''"); return;}
if (txt.indexOf("]") != -1){
alert("Search is invalid! Contains a ']''"); return;}
if (txt.indexOf(";") != -1){
alert("Search is invalid! Contains a ';''"); return;}
if (txt.indexOf(":") != -1){
alert("Search is invalid! Contains a ':''"); return;}
if (txt.indexOf("<") != -1){
alert("Search is invalid! Contains a '<''"); return;}
if (txt.indexOf(">") != -1){
alert("Search is invalid! Contains a '>''"); return;}
if (txt.indexOf("?") != -1){
alert("Search is invalid! Contains a '?''"); return;}
if (txt.indexOf("-") != -1){
alert("Search is invalid! Contains a '-''"); return;}
if (txt.indexOf("=") != -1){
alert("Search is invalid! Contains a '=''"); return;}
if (txt.indexOf("+") != -1){
alert("Search is invalid! Contains a '+''"); return;}
else {
jsi = new makeLinks(linksize);
searchLinks(jsi, txt);
document.write("<hr>");
document.write("<a href='searchabledb2.htm'>返回检索页</a><br><br> ");
//将searchabledb2.htm替换为检索页的URL,以便访问者返回检索页。
}
}
</script>
php爱好者站 http://www.phpfans.net PHP|MySQL|javascript|ajax|html.