Some time ago, I wanted to use Google Suggest and found a lot of results on the Internet but was not satisfactory, so I spent some time writing one. It is basically the same as Google Suggest. The subsequent approximate results are beyond the scope of this program. For a more complete code, please refer to this version that supports database. After editing and testing by Script House.
Asp+Ajax imitation google search prompt effect database version
There are places that need to be modified:
Copy the code code as follows:
javascript.js
var url=ajax.asp; //Backend address
var time_delayajax=300; //Search delay
var time_delayupdown=100; //Direction key delay
obj_div.style.top = (xtop + 20) + px; //20 is almost the height of the input box, please adjust it according to the actual situation
ajax_xmlhttp.send(sift_value=+escape(temp_value)); //The value submitted to the background
dd=d+<li onmouseover=/overli(+i+);/ onmousedown=/downli(+i+)/ onmouseup=/upli(+i+,event)/ onmousemove=/moveli();/><span>about +c [1]+result</span>+c[0]+</li>;//Display of ****li
The background output result format must be 'Text 1, Text 2'....
'java,2''javascript,11''java example,22'etc.
default.css
Copy the code code as follows:
.ajaxsearch {
width:300px; //The width of the prompt layer
}
Homeindex.html
Copy the code code as follows:
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<meta http-equiv=Content-Type content=text/html; charset=utf-8 />
<link rel=stylesheet type=text/css href=default.css />
<script language=JavaScript src=javascript.js type=text/javascript></script>
<title>Google suggest high imitation example</title>
</head>
<body onResize=removediv();>
<div style=margin:20px 50px>
<input style=width:298px;height:18px type=text autocomplete=off onBlur=blurdeal(); onKeyDown=keydowndeal(event); onFocus=focusdeal(event); />
</div>
</body>
</html>
Scriptjavascript.js
Copy the code code as follows:
/////////////////////////////////Search prompt box///////////////// //////////////////
var obj_div; //Prompt layer object
var obj_input; //Input box object
var main_delay; //Judge value change delay object
var ajax_delay; //ajax delayed search object
var updown_delay; //Direction key delay object
var ajax_xmlhttp; //ajax object
var div_word=null; //Search value corresponding to the current prompt layer
var li_num=-1; //Pseudo cursor position, prompts the li number of the selected layer, starting from 0
var li_down=-1; //The sequence number of the prompt layer when the mouse is pressed
var value_ed=; //The value of the input box before delay
var value_ing=; //The current value of the input box
var value_unexit=; //Search for the beginning of a value with no results
var updown_run=false; //Allow arrow keys up and down
var ajax_run=false; //true means normal process, false stops ajax
var ajax_run_ing=false; //true is running, false is idle
var input_focus=false; //Text box focus
var url=ajax.asp; //Backend address *************************************** *********************
var time_delayajax=300; //Search delay********************************************** *******************
var time_delayupdown=100; //Direction key delay****************************************** ****************
var $=function(Fun_id){
return document.getElementById(Fun_id);
}
try{
ajax_xmlhttp= new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
try{
ajax_xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');
}catch(e){
try{
ajax_xmlhttp= new XMLHttpRequest();
}catch(e){ajax_xmlhttp=null;}
}
}
/////////////////////////Create prompt layer//////////////////////// /
function createajaxdiv(){
var create_div = document.createElement(div);
create_div.type = div;
var promptdiv = document.body.appendChild(create_div);
promptdiv.className = ajaxsearch;
obj_div=promptdiv;
}
//////////////////////////Set the prompt layer position/////////////////////// //
function removediv(){
if(!obj_div || !obj_input)return false;
if(obj_div.style.display==none)return false;
var obj=obj_input;
var xtop=0;
var xleft=0;
while(obj){
xtop += obj[offsetTop];
xleft += obj[offsetLeft];
obj = obj.offsetParent;
}
obj_div.style.left = xleft + px;
obj_div.style.top = (xtop + 20) + px; //20 is almost the height of the input box, please adjust it according to the actual situation********************** ***************************************8
li_down=-1;
}
/////////////////////////Hide prompt layer//////////////////////// /
function hideajaxdiv(){
obj_div.style.display=none;
li_down=-1;
}
//////////////////////////Set the selected <li>css style/////////////////// //////
function setlistyle(){
for(var i=0;i<obj_div.firstChild.childNodes.length;i++){
obj_div.firstChild.childNodes[i].id=;
}
if(li_num!=-1)obj_div.firstChild.childNodes[li_num].id=liseleted;
}
//////////////////////////The mouse passes through the prompt layer/////////////////////// //
function overli(Fun_seletedlinum){
if(li_num==-1)value_ing=obj_input.value;
li_num=Fun_seletedlinum;
setlistyle();
}
//////////////////////////Mouse drag prompt layer////////////////////// ///
function moveli(){
if(window.getSelection){
setfocus();
window.getSelection().removeAllRanges();
}else{
document.selection.empty();
setfocus();
}
}
//////////////////////////Mouse press prompt layer////////////////////// ///
function downli(Fun_seletedlinum){
if(!obj_input)return false;
li_down=Fun_seletedlinum;
input_focus=true;
}
//////////////////////////Mouse pop-up prompt layer////////////////////// ///
function upli(Fun_seletedlinum,Fun_event){
if(!obj_input)return false;
if(Fun_event.button==2){li_down=-1;return}
if(li_down!=Fun_seletedlinum){
li_down=-1;
return false;
}
clearTimeout(ajax_delay);
clearTimeout(updown_delay);
updown_run=true;
ajax_run=false;
ajax_run_ing=false;
li_num=-1;
div_word=null;
value_ed=obj_div.firstChild.childNodes[Fun_seletedlinum].childNodes[1].nodeValue;
obj_input.value=value_ed;
value_ing=value_ed;
hideajaxdiv();
obj_div.innerHTML=;
}
//////////////////////////Set the text box to get focus////////////////////// //
function setfocus(){
if(window.event){
var r = obj_input.createTextRange();
r.moveStart('character',obj_input.value.length);
r.collapse(true);
r.select();
}else{
obj_input.selectionStart=obj_input.value.length;
obj_input.focus();
}
}
//////////////////////////The text box loses focus/////////////////////// //
function blurdeal(){
if(input_focus==true){
setfocus();
setTimeout(setfocus());
return false;
}
updown_run=false;
ajax_run=false;
ajax_run_ing=false;
clearInterval(main_delay);
clearTimeout(ajax_delay);
clearTimeout(updown_delay);
hideajaxdiv();
if(value_ed!=obj_input.value)obj_div.innerHTML=;
}
//////////////////////////Text box gets focus/////////////////////// //
function focusdeal(Fun_event){
if(!obj_div)createajaxdiv();
if(input_focus==true){
input_focus=false;
return false;
}
var obj=((window.event)?Fun_event.srcElement:Fun_event.target);
if(obj.type!=text)return false;
updown_run=true;
ajax_run=true;
ajax_run_ing=false;
if(obj_input==obj && value_ed==obj.value && obj_div.innerHTML!=){
obj_div.style.display=block;
removediv();
}else{
obj_input=obj;
value_ed=obj.value;
value_ing=obj.value;
value_unexit=;
li_num=-1;
li_down=-1;
div_word=null;
obj_div.innerHTML=;
removediv();
}
main_delay=setInterval(mainajax(),10);
}
/////////////////////////Main function //////////////////////////
function mainajax(){
if(value_ed==obj_input.value)return false;
if(value_unexit!= && (obj_input.value).indexOf(value_unexit)==0){hideajaxdiv();obj_div.innerHTML=;return false;}
if(value_ed!=obj_input.value && ajax_run_ing==false){
ajax_run=true;
value_ed=obj_input.value;
clearTimeout(ajax_delay);
if(obj_input.value!=){
ajax_delay=setTimeout(getsearch();,time_delayajax);
}else{
hideajaxdiv();
obj_div.innerHTML=;
ajax_run=false;
return false;
}
}
}
/////////////////////////Get search content///////////////////////// /
function getsearch(){
var temp_value=obj_input.value;
if(ajax_xmlhttp==null){
return false;
}else if(ajax_xmlhttp.readyState!=0){
ajax_xmlhttp.abort();
ajax_run_ing=false;
}
ajax_xmlhttp.onreadystatechange=function(){
if(ajax_run==false){ajax_xmlhttp.abort();ajax_run_ing=false;return false;}
if(ajax_xmlhttp.readyState==4){
obj_div.innerHTML=;
if(ajax_xmlhttp.status==200 || ajax_xmlhttp.status==304){
var content=ajax_xmlhttp.responseText;
if(contant!= && ajax_run==true){
div_word=temp_value;
obj_div.innerHTML=resetcontant(contant);
obj_div.style.display=block;
removediv();removediv();
}else{
hideajaxdiv();
}
updown_run=true;
ajax_run_ing=false;
li_num=-1;
if(contant==)value_unexit=temp_value;
}
}
}
ajax_xmlhttp.open(post,url,true);
ajax_xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
ajax_run_ing=true;
ajax_xmlhttp.send(sift_value=+escape(temp_value)); //The value submitted to the background****************************** ***********
}
/////////////////////////Content reorganization/////////////////////////
function resetcontant(Fun_contant){
if(Fun_contant==null || Fun_contant==)return ;
var a=Fun_contant.substring(1,Fun_contant.length-1);
if(Fun_contant==null || Fun_contant==)return ;
var b=a.split('');
var c;
vard;
d=<ul>;
for(var i in b){
c=b[i].split(,);
//************************************************ ***************
d=d+<li onmouseover=/overli(+i+);/ onmousedown=/downli(+i+)/ onmouseup=/upli(+i+,event)/ onmousemove=/moveli();/><span>about +c [1]+result</span>+c[0]+</li>;
//************************************************ ***************
}
d=d+<li onmousedown=/input_focus=true;li_down=-1;/ onmouseup=/li_down=-1/ onmousemove=/moveli();/><span><a class=/shutajaxdiv/ onclick=/hideajaxdiv( );/>Close</a></span></li>
d=d+</ul>;
return d;
}
//////////////////////////Keyboard events//////////////////////////
function keydowndeal(Fun_event){
var keyc=((window.event)?Fun_event.keyCode:Fun_event.which);
if(keyc==13){hideajaxdiv();return false;}
if(keyc==27){
if(obj_div.style.display==block && li_num>-1)value_ed=obj_input.value=value_ing;
hideajaxdiv();
return false;
}
if(keyc==40 || keyc==38){
if(div_word==obj_input.value && obj_div.style.display==none && obj_div.innerHTML!=){
obj_div.style.display=block;
removediv();
return false;
}
if(li_num==-1){
if(div_word!=obj_input.value)return false;
}else{
if(div_word!=value_ing)return false;
}
if(updown_run==false || ajax_run_ing==true || obj_div.style.display==none)return false;
updown_delay=setTimeout(updownli(+keyc+),time_delayupdown);
updown_run=false;
}
}
//////////////////////////Direction key movement li/////////////////////// //
function updownli(Fun_key){
if(!obj_div){return false;}
updown_run=true;
if(li_num==-1){
if(div_word!=obj_input.value){hideajaxdiv();obj_div.innerHTML=;li_num=-1;return false;}
}else{
if(div_word!=value_ing){hideajaxdiv();obj_div.innerHTML=;li_num=-1;return false;}
}
if(updown_run==false)return false;
if(li_num==-1)value_ing=value_ed;
if(Fun_key==40){
if(li_num<obj_div.firstChild.childNodes.length-2){
li_num++;
}else{
li_num=-1;
}
}
if(Fun_key==38){
if(li_num>=0){
li_num--;
}else{
li_num=obj_div.firstChild.childNodes.length-2;
}
}
if(li_num!=-1){
value_ed=obj_input.value=obj_div.firstChild.childNodes[li_num].childNodes[1].nodeValue;
}else{
value_ed=obj_input.value=value_ing;
}
setlistyle();
}
background ajax.asp
Copy the code code as follows:
<%@LANGUAGE=VBSCRIPT CODEPAGE=65001 %>
<%option explicit%>
<%Response.CodePage=65001%>
<%Response.Charset=utf-8 %>
<!--#include file=conn.asp-->
<%
dimSift_value
dim Sql,Rs,I,Num
dimContant
Content=
Num=10
Sift_value=replace(unescape(request.form(sift_value)),,)
Sql=select top &Num& keyword,matchnum from search where keyword like &Sift_value&% order by id
set Rs=server.CreateObject(adodb.recordset)
Rs.open Sql,Conn,1,1
if not (Rs.eof and Rs.bof) then
for I=0 to Num-1
Contant=Contant&'&rs(0)&,&rs(1)&'
Rs. movenext
if Rs.eof then exit for
next
end if
response.Write(Contant)
Rs.close
set Rs=nothing
%>