ASP+AJAX做類似Google的搜索提示

2010-08-28 10:54:03來源:西部e網(wǎng)作者:

Google搜索的時(shí)候出現(xiàn)提示的功能很酷,也很實(shí)用,那么如果用ASP和AJAX做成同樣的效果呢?下面icech找到一篇文章介紹,建議大家參考一下!

主要要文件有:

Index.html 實(shí)現(xiàn)功能,一個(gè)文本框,輸入內(nèi)容并實(shí)現(xiàn)提示

search.asp 查詢功能,讓文本框輸入的內(nèi)容在數(shù)據(jù)庫中查詢,然后返回給客戶端

conn.asp 數(shù)據(jù)庫連接功能,實(shí)現(xiàn)與數(shù)據(jù)庫相連 xmlhttp.js AJAX 核心部分,用來把客戶端的數(shù)據(jù)傳給服務(wù)端,再把服務(wù)端的數(shù)據(jù)返還給客戶端.

style.css 樣式文件,主要是對(duì)google提示框查詢到的內(nèi)容進(jìn)行樣式化處理(weste)

先看第一個(gè)文件style.css

@charset "utf-8";
/* CSS Document */

body
{
font-size:12px;
font-family:Arial, Helvetica, sans-serif;
}

#search_suggest
{
position:absolute;
background:#FFFFFF;
text-align:left;
border:1px #000000 solid;
}

.suggest_link_over
{
background-color:#e8f2fe;
padding:2px 6px;
}

.suggest_link
{
padding:2px 6px;
background-color:#FFFFFF;
}

.none
{
display:none;
}

第二個(gè)文件: xmlhttp.js

// JavaScript Documentrt
var xmlHttp = createXmlHttpRequest();

function createXmlHttpRequest()
{
var xmlhttp = null;
try
{
xmlhttp = XMLHttpRequest();
}
catch(e1)
{
try
{
xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
}
catch(e2)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e3)
{
xmlhttp = false;
alert("創(chuàng)建失敗!");
}
}
}

return xmlhttp;
}

function change_key()
{
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
{
var str = document.getElementById("txt_key").value;

xmlHttp.open("get","search.asp?key=" + str ,true);
xmlHttp.onreadystatechange = handSearchRequest;
xmlHttp.send(null);
}
}

function handSearchRequest()
{
if (xmlHttp.readyState == 4)
{
var div = document.getElementById("search_suggest");
div.innerHTML = "";
var str = xmlHttp.responseText.split("|");

for (var i=0; i<str.length; i++)
{
var suggest = ’<div onmouseover="javascript:suggestOver(this);" ’;
suggest += ’onmouseout="javascript:suggestOut(this);" ’;
suggest += ’onclick="javascript:setSearch(this.innerHTML);" ’;
suggest += ’class="suggest_link">’ + str[i] + ’</div>’;
div.innerHTML += suggest;
}
}
}

function setSearch(div_value)
{
document.getElementById("txt_key").value = div_value;
document.getElementById("search_suggest").className = ’none’;
}
function suggestOver(div_value)
{
div_value.className = ’suggest_link_over’;
}

function suggestOut(div_value)
{
div_value.className = ’suggest_link’;
}

第三個(gè)文件:conn.asp

<%
set conn = Server.CreateObject("Adodb.Connection")
connStr = "Provider=SQLOLEDB;Server=.\SQLEXPRESS;UID=sa;PWD=sa;Initial catalog=test;"
conn.ConnectionString = connStr
conn.open
%>

第四個(gè)文件:search.asp

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!-- #include file="conn.asp" -->
<%
key = request.QueryString("key")

if (key <> "") then
key = replace(key,"\","")
key = replace(key,"’","")
key = replace(key,"or","")
sel_sql = "select [key] from [key] where [key] like ’" & key & "%’"
dim keyword
keyword = ""

set rs = conn.execute (sel_sql)
do while not rs.eof
keyword = keyword & rs(0) & "|"
rs.movenext
loop

response.Write(keyword)
end if
%>

最后一個(gè)結(jié)果文件:index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>google提示</title>
<script src="xmlhttp.js" ></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<input name="txt_key" type="text" id="textarea" autocomplete="off" onkeyup="change_key()"/><br/>
<div id="search_suggest" ></div>
</form>
</body>
</html>